python - How to figure out which command line parameters have been set in plac? -
my python
script takes configuration values in order:
- command line argument (possibility overwrite user defined values in configuration file)
- configuration file value (user defined values)
- default value in source code
i need figure out option has been set on command line in order determine whether default value has been set explicitly or not. plac [not?] transparent , don't see how it's possible. avoid parse sys.argv
because writing command line parser in order use command line parser doesn't seem idea.
i'm using plac
0.9.1 on ubuntu 15.04
.
could give simple example of plac
setup? used know well, know underlying argparse
better.
are using plac
invoke function(s) directly, e.g.
plac.call(main)
internally plac
creates argparse.argumentparser
(actually own subclass), populates arguments derived functions signature. , calls function values parsed.
if working argparse
directly, create parser, populate it, , invoke with
args = parser.parse_args()
args
namespace
object attributes named after arguments. can converted dictionary.
if go argparse
route (possibly still creating parser plac
), can check attributes in args
, , compare values defaults, or config
file values.
ipython
populates argparse.parser
arguments derived config files (default , custom). lets on ride config defaults @ several stages - custom files or commandline.
plac
supposed easier argparse
, integrating parser creation , function calling. in case may better separate steps.
the code plac.call
is:
def call(obj, arglist=sys.argv[1:], eager=true): ... cmd, result = parser_from(obj).consume(arglist) ...
the first part creates parser; arguments of parser based on annotations decorator. main
in test plac
documentation.
in [22]: p=plac.parser_from(main) in [23]: p out[23]: argumentparser(prog='ipython2.7', usage=none, description=none, version=none, formatter_class=<class 'argparse.rawdescriptionhelpformatter'>, conflict_handler='error', add_help=true)
p.consume
longer function, checks things subparsers. @ point does
ns, extraopts = self.parse_known_args(arglist) # or self.parse_args
ns
argparse namespace, parameters set parser - both defaults , ones command line. calls 'the function' values ns
(split python
positional args , keyword args).
so can call parser directly:
in [25]: p.parse_args([]) # or default sys.argv[1:] out[25]: namespace(args=[], kw={}, opt=none)
the p.print_help()
shows arguments expects commandline, in neatly formatted form.
and if want further argparse
guts can @ p._actions
, list of argparse.action
objects uses parse commandline.
Comments
Post a Comment