From 5efe1a09ce55dca4af4da2a8e6f86606213e6c33 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Thu, 4 Jun 2020 17:27:43 +0200 Subject: [PATCH] docs/argparse: Add note on optional arguments Yeah I had to answer this one again. [ci skip] --- doc_src/cmds/argparse.rst | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/doc_src/cmds/argparse.rst b/doc_src/cmds/argparse.rst index 354b6b948..979b79fd2 100644 --- a/doc_src/cmds/argparse.rst +++ b/doc_src/cmds/argparse.rst @@ -103,6 +103,31 @@ See the :ref:`fish_opt ` command for a friendlier but more verbose In the following examples if a flag is not seen when parsing the arguments then the corresponding _flag_X var(s) will not be set. +Note: Optional arguments +------------------------ + +An option defined with ``=?`` can take optional arguments. Optional arguments have to be *directly attached* to the option they belong to. + +That means you can only call:: + + cmd --flag=value + # or + cmd -fvalue + +but not:: + + cmd --flag value + # "value" here will be used as a positional argument and "--flag" won't have an argument. + +If this weren't the case, using an option without an optional argument would be difficult if you also wanted to use positional arguments. + +For example:: + + grep --color auto + # Here "auto" will be used as the search string, "color" will not have an argument and will fall back to the default + +This isn't specific to argparse but common to all things using ``getopt(3)`` (if they have optional arguments at all). + Flag Value Validation ---------------------