├── tests ├── test.nim └── config.nims ├── .gitignore ├── bossy.nimble ├── examples └── basic.nim ├── .github └── workflows │ ├── build.yml │ └── docs.yml ├── LICENSE ├── README.md └── src └── bossy.nim /tests/test.nim: -------------------------------------------------------------------------------- 1 | import bossy 2 | -------------------------------------------------------------------------------- /tests/config.nims: -------------------------------------------------------------------------------- 1 | --path:"../src" 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # ignore files with no extention: 2 | * 3 | !*/ 4 | !*.* 5 | 6 | # normal ignores: 7 | *.exe 8 | nimcache 9 | *.pdb 10 | *.ilk 11 | .* 12 | -------------------------------------------------------------------------------- /bossy.nimble: -------------------------------------------------------------------------------- 1 | version = "0.1.0" 2 | author = "Ryan Oldenburg" 3 | description = "Makes supporting command line arguments easier" 4 | license = "MIT" 5 | 6 | srcDir = "src" 7 | 8 | requires "nim >= 1.6.10" 9 | -------------------------------------------------------------------------------- /examples/basic.nim: -------------------------------------------------------------------------------- 1 | import bossy 2 | 3 | ## Get the command line arguments, parsed by Bossy 4 | let args = getCommandLineArgs() 5 | 6 | ## Check if an argument is present 7 | echo "flag" in args 8 | 9 | ## Get the value for an argument 10 | echo args["port"] 11 | 12 | ## Get the value for an argument, or use a default value 13 | echo args.getOrDefault("port", "8080") 14 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Github Actions 2 | on: [push, pull_request] 3 | jobs: 4 | build: 5 | strategy: 6 | fail-fast: false 7 | matrix: 8 | os: [ubuntu-latest, windows-latest] 9 | nim-version: ['1.6.10', '1.6.x', 'stable'] 10 | 11 | runs-on: ${{ matrix.os }} 12 | 13 | steps: 14 | - uses: actions/checkout@v2 15 | - uses: jiro4989/setup-nim-action@v1 16 | with: 17 | nim-version: ${{ matrix.nim-version }} 18 | repo-token: ${{ secrets.GITHUB_TOKEN }} 19 | - run: nimble test -y 20 | - run: nimble test --gc:orc -y 21 | -------------------------------------------------------------------------------- /.github/workflows/docs.yml: -------------------------------------------------------------------------------- 1 | name: docs 2 | on: 3 | push: 4 | branches: 5 | - master 6 | env: 7 | nim-version: 'stable' 8 | nim-src: src/${{ github.event.repository.name }}.nim 9 | deploy-dir: .gh-pages 10 | jobs: 11 | docs: 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v3 15 | - uses: jiro4989/setup-nim-action@v1 16 | with: 17 | nim-version: ${{ env.nim-version }} 18 | - run: nimble install -Y 19 | - run: nimble doc --index:on --project --git.url:https://github.com/${{ github.repository }} --git.commit:master --out:${{ env.deploy-dir }} ${{ env.nim-src }} 20 | - name: "Copy to index.html" 21 | run: cp ${{ env.deploy-dir }}/${{ github.event.repository.name }}.html ${{ env.deploy-dir }}/index.html 22 | - name: Deploy documents 23 | uses: peaceiris/actions-gh-pages@v3 24 | with: 25 | github_token: ${{ secrets.GITHUB_TOKEN }} 26 | publish_dir: ${{ env.deploy-dir }} 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2022 Ryan Oldenburg 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bossy 2 | 3 | `nimble install bossy` 4 | 5 | ![Github Actions](https://github.com/guzba/bossy/workflows/Github%20Actions/badge.svg) 6 | 7 | [API reference](https://guzba.github.io/bossy/) 8 | 9 | Bossy is a simple repo that makes working with command line arguments a little easier. 10 | 11 | All Bossy does is parse the command line arguments using [std/parseopt](https://nim-lang.org/docs/parseopt.html) and give you a simpler interface to work with. 12 | 13 | Bossy is great for supporting command line parameters without any magic. 14 | 15 | If you want a powerful library for building command line interfaces, Bossy currently isn't that. There are other options on [Nimble](https://nimble.directory/). 16 | 17 | ## Example 18 | 19 | ```nim 20 | import bossy 21 | 22 | ## Get the command line arguments, parsed by Bossy 23 | let args = getCommandLineArgs() 24 | 25 | ## Check if an argument is present 26 | echo "flag" in args 27 | 28 | ## Get the value for an argument 29 | echo args["port"] 30 | 31 | ## Get the value for an argument, or use a default value 32 | echo args.getOrDefault("port", "8080") 33 | ``` 34 | -------------------------------------------------------------------------------- /src/bossy.nim: -------------------------------------------------------------------------------- 1 | import std/parseopt, std/os, std/typetraits 2 | 3 | type CommandLineArgs* = distinct seq[(string, string)] 4 | 5 | converter toBase*(args: var CommandLineArgs): var seq[(string, string)] = 6 | args.distinctBase 7 | 8 | converter toBase*(args: CommandLineArgs): lent seq[(string, string)] = 9 | args.distinctBase 10 | 11 | proc `[]`*(args: CommandLineArgs, key: string): string = 12 | ## Get a key out of the args. Case-sensitive. 13 | ## Returns an empty string if key is not present. 14 | ## Use a for loop to get duplicate keys. 15 | for (k, v) in args.toBase: 16 | if k == key: 17 | return v 18 | 19 | proc contains*(args: CommandLineArgs, key: string): bool = 20 | ## Returns true if key is in the args. Case-sensitive. 21 | for pair in args: 22 | if pair[0] == key: 23 | return true 24 | 25 | proc getOrDefault*(args: CommandLineArgs, key, default: string): string = 26 | ## Retrieves the value for key if it is in args, otherwise `default` is returned. 27 | ## Note, if key is present in args but the value is an empty string, that 28 | ## empty string will be returned instead of `default`. 29 | if key in args: 30 | result = args[key] 31 | else: 32 | result = default 33 | 34 | proc getCommandLineArgs*(): CommandLineArgs = 35 | ## Returns the parsed command line arguments. See 36 | ## https://nim-lang.org/docs/parseopt.html for the supported syntax. 37 | var opt = initOptParser(quoteShellCommand(commandLineParams())) 38 | while true: 39 | opt.next() 40 | case opt.kind: 41 | of cmdEnd: 42 | break 43 | of cmdShortOption, cmdLongOption: 44 | result.add((opt.key, opt.val)) 45 | of cmdArgument: 46 | result.add((opt.key, "")) 47 | --------------------------------------------------------------------------------