Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples: Parsing sets/flags and enums #22

Closed
mratsim opened this issue Apr 12, 2018 · 3 comments
Closed

Examples: Parsing sets/flags and enums #22

mratsim opened this issue Apr 12, 2018 · 3 comments

Comments

@mratsim
Copy link

mratsim commented Apr 12, 2018

I used cligen for nimbus-launch, a nim project skeleton generator. First of all, awesome work.

Here are some snippets that can be reused as example to parse the following types from the CLI.

type
  License* = enum
    MIT, Apachev2, GPLv2, GPLv3

  Licenses* = set[License]

  TravisConfig* = enum
    StatusDocker, Generic
template argParse*(dst: Licenses, key: string, val: string, help: string) =
  # Parse license input
  let args = val.split(',')
  dst = {}
  for input_license in args:
    var isValid: bool = false
    for supported_license in low(License)..high(License):       # Interesting read: "parseEnum is slow" https://forum.nim-lang.org/t/2949
      if cmpIgnoreStyle(input_license, $supported_license) == 0:
        incl(dst, supported_license)
        isValid = true
    if not isValid:
      argRet(1, "Wrong input license(s) for param \"$1\"\n$2, only MIT, Apachev2, GPLv2 and GPLv3 are supported." %
             [key, help])

template argHelp*(helpT: seq[array[0..3, string]], defVal: Licenses,
                  parNm: string, sh: string, parHelp: string) =
  helpT.add([ keys(parNm, sh), "Licenses", $defVal, parHelp ])


template argParse*(dst: TravisConfig, key: string, val: string, help: string) =
  # Parse TravisConfig input
  var isValid: bool = false
  for supported_config in low(TravisConfig)..high(TravisConfig):       # Interesting read: "parseEnum is slow" https://forum.nim-lang.org/t/2949
    if cmpIgnoreStyle(val, $supported_config) == 0:
      dst = supported_config
      isValid = true
  if not isValid:
    argRet(1, "Wrong input travis config for param \"$1\"\n$2, only StatusDocker and Generic are supported." %
            [key, help])

template argHelp*(helpT: seq[array[0..3, string]], defVal: TravisConfig,
                  parNm: string, sh: string, parHelp: string) =
  helpT.add([ keys(parNm, sh), "Travis: ", $defVal, parHelp ])
@c-blake
Copy link
Owner

c-blake commented Apr 12, 2018

Hey, Mamy. Thanks. Glad you like it. Your code looks good. I should probably have some example code for doing enums.

@c-blake
Copy link
Owner

c-blake commented Apr 27, 2018

Hey. So, I added generic support for any enums. See test/Enums.nim. I have also changed things in a few backward incompatible ways for people defining their own argParse/argHelp like you (see RELEASE_NOTES.md) and added support for seq[T] for almost all the regular T supported by argcvt. I ran into some trouble supporting seq[enum], though...Internal compiler error. Similar generic support for set[T] is probably not so bad, but not as widely applicable since T is more restricted. The internal error has me regretting a bit using templates instead of generic functions, but then the way I let CLI authors pick both the value and type of seqDelimit has me not regretting.

@c-blake
Copy link
Owner

c-blake commented May 2, 2018

Ok. Both any enums and any sets of enums should just work by default now. You should be able to just comment out/delete your definitions. There is also a new "+="/"-=" syntax for updating existing values as in --licenses+=MIT. So, I am closing this issue.

I don't know if you have any other user-defined argParse and argHelp, but there have been major changes in how they work. See RELEASE_NOTES.md or argcvt.nim or DETAILS.md. I will soon tag some new release version to cover that backward incompatibility.

@c-blake c-blake closed this as completed May 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants