parseopt.nim documentation clarity - default values & cmdEnd in getopt (#21047)
parseopt.nim documentation clarity Added example for default values to cmd line parameters. Additionally, added lines in getopt documentation about case switching still requiring the `cmdEnd` kind. Hopefully this clears up any vagueness for those following along in the example but omitting the `cmdEnd` in the case because the documentation said it wasn't needed.
This commit is contained in:
parent
b68b28fd24
commit
8be46f3d16
1 changed files with 27 additions and 2 deletions
|
|
@ -75,6 +75,27 @@
|
||||||
## The `getopt iterator<#getopt.i,OptParser>`_, which is provided for
|
## The `getopt iterator<#getopt.i,OptParser>`_, which is provided for
|
||||||
## convenience, can be used to iterate through all command line options as well.
|
## convenience, can be used to iterate through all command line options as well.
|
||||||
##
|
##
|
||||||
|
## To set a default value for a variable assigned through `getopt` and accept arguments from the cmd line.
|
||||||
|
## Assign the default value to a variable before parsing.
|
||||||
|
## Then set the variable to the new value while parsing.
|
||||||
|
##
|
||||||
|
## Here is an example:
|
||||||
|
## .. code-block::
|
||||||
|
## import std/parseopt
|
||||||
|
##
|
||||||
|
## var varName: string = "defaultValue"
|
||||||
|
##
|
||||||
|
## for kind, key, val in getopt():
|
||||||
|
## case kind
|
||||||
|
## of cmdArgument:
|
||||||
|
## discard
|
||||||
|
## of cmdLongOption, cmdShortOption:
|
||||||
|
## case key:
|
||||||
|
## of "varName": # --varName:<value> in the console when executing
|
||||||
|
## varName = val # do input sanitization in production systems
|
||||||
|
## of cmdEnd:
|
||||||
|
## discard
|
||||||
|
##
|
||||||
## `shortNoVal` and `longNoVal`
|
## `shortNoVal` and `longNoVal`
|
||||||
## ============================
|
## ============================
|
||||||
##
|
##
|
||||||
|
|
@ -210,6 +231,8 @@ proc initOptParser*(cmdline = "", shortNoVal: set[char] = {},
|
||||||
## parameters<#nimshortnoval-and-nimlongnoval>`_ for more information on
|
## parameters<#nimshortnoval-and-nimlongnoval>`_ for more information on
|
||||||
## how this affects parsing.
|
## how this affects parsing.
|
||||||
##
|
##
|
||||||
|
## This does not provide a way of passing default values to arguments.
|
||||||
|
##
|
||||||
## See also:
|
## See also:
|
||||||
## * `getopt iterator<#getopt.i,OptParser>`_
|
## * `getopt iterator<#getopt.i,OptParser>`_
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
|
|
@ -403,7 +426,8 @@ iterator getopt*(p: var OptParser): tuple[kind: CmdLineKind, key,
|
||||||
## Convenience iterator for iterating over the given
|
## Convenience iterator for iterating over the given
|
||||||
## `OptParser<#OptParser>`_.
|
## `OptParser<#OptParser>`_.
|
||||||
##
|
##
|
||||||
## There is no need to check for `cmdEnd` while iterating.
|
## There is no need to check for `cmdEnd` while iterating. If using `getopt`
|
||||||
|
## with case switching, checking for `cmdEnd` is required.
|
||||||
##
|
##
|
||||||
## See also:
|
## See also:
|
||||||
## * `initOptParser proc<#initOptParser,string,set[char],seq[string]>`_
|
## * `initOptParser proc<#initOptParser,string,set[char],seq[string]>`_
|
||||||
|
|
@ -451,7 +475,8 @@ iterator getopt*(cmdline: seq[string] = @[],
|
||||||
## parameters<#nimshortnoval-and-nimlongnoval>`_ for more information on
|
## parameters<#nimshortnoval-and-nimlongnoval>`_ for more information on
|
||||||
## how this affects parsing.
|
## how this affects parsing.
|
||||||
##
|
##
|
||||||
## There is no need to check for `cmdEnd` while iterating.
|
## There is no need to check for `cmdEnd` while iterating. If using `getopt`
|
||||||
|
## with case switching, checking for `cmdEnd` is required.
|
||||||
##
|
##
|
||||||
## See also:
|
## See also:
|
||||||
## * `initOptParser proc<#initOptParser,seq[string],set[char],seq[string]>`_
|
## * `initOptParser proc<#initOptParser,seq[string],set[char],seq[string]>`_
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue