summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorRussell Brinson <57331546+rbrins@users.noreply.github.com>2023-01-11 12:20:39 -0500
committerGitHub <noreply@github.com>2023-01-11 12:20:39 -0500
commit8be46f3d16fc4c3b05d927aa1e9738ac865a266e (patch)
treeae51ae8ec5f555a2678c9197fc5ca2e402752767
parentb68b28fd2448b15e989b9e8b07e33eee4f9b8822 (diff)
downloadNim-8be46f3d16fc4c3b05d927aa1e9738ac865a266e.tar.gz
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.
-rw-r--r--lib/pure/parseopt.nim29
1 files changed, 27 insertions, 2 deletions
diff --git a/lib/pure/parseopt.nim b/lib/pure/parseopt.nim
index f8d03e092..9052067db 100644
--- a/lib/pure/parseopt.nim
+++ b/lib/pure/parseopt.nim
@@ -74,7 +74,28 @@
 ##
 ## The `getopt iterator<#getopt.i,OptParser>`_, which is provided for
 ## 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`
 ## ============================
 ##
@@ -210,6 +231,8 @@ proc initOptParser*(cmdline = "", shortNoVal: set[char] = {},
   ## parameters<#nimshortnoval-and-nimlongnoval>`_ for more information on
   ## how this affects parsing.
   ##
+  ## This does not provide a way of passing default values to arguments. 
+  ##
   ## See also:
   ## * `getopt iterator<#getopt.i,OptParser>`_
   runnableExamples:
@@ -403,7 +426,8 @@ iterator getopt*(p: var OptParser): tuple[kind: CmdLineKind, key,
   ## Convenience iterator for iterating over the given
   ## `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:
   ## * `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
   ## 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:
   ## * `initOptParser proc<#initOptParser,seq[string],set[char],seq[string]>`_