Proposed argument changes to ntpq (fixing bug #319)
Gary E. Miller
gem at rellim.com
Thu Jun 8 22:29:43 UTC 2017
Yo Ian!
On Thu, 8 Jun 2017 15:29:19 -0500
Ian Bruene via devel <devel at ntpsec.org> wrote:
> >> "A <colon> (':') shall be returned if getopt() detects a missing
> >> argument and the first character of optstring was a <colon> (':')."
> >>
> >> So optional optarg can be valid POXIX.
> > I didn't know that. Thanks.
>
> Unfortunately the documentation / StackOverflow questions / library
> code I've been able to find indicate that this feature does not exist
> in python's getopt.
NTPsec does not use Python's getopt(). It uses argparse().
https://docs.python.org/3/library/argparse.html
Of interest is Argumentparser.add_argument():
https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_argument
Check out the second bullit under 16.4.3.3 nargs:
"'?'. One argument will be consumed from the command line if possible,
and produced as a single item. If no command-line argument is present,
the value from default will be produced. "
Then example code for what I suggested.
Simplied example code here:
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', nargs='?', const='c', default='d')
_StoreAction(option_strings=['--foo'], dest='foo', nargs='?', const='c', default='d', type=None, choices=None, help=None, metavar=None)
>>> parser.parse_args(['--foo', 'YY'])
Namespace(foo='YY')
>>> parser.parse_args(['--foo'])
Namespace(foo='c')
RGDS
GARY
---------------------------------------------------------------------------
Gary E. Miller Rellim 109 NW Wilmington Ave., Suite E, Bend, OR 97703
gem at rellim.com Tel:+1 541 382 8588
Veritas liberabit vos. -- Quid est veritas?
"If you can’t measure it, you can’t improve it." - Lord Kelvin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <https://lists.ntpsec.org/pipermail/devel/attachments/20170608/29dad311/attachment.bin>
More information about the devel
mailing list