python - Have argparse collect, but not respond to, flags -
I have a script that takes some arguments, to select a script to use some arguments, And the rest passes the arguments of that script so it looks like this:
parser = argument Parser () parser.add_argument ('script', option ['a', 'b' ]) Parser.add_argument ('rest_args', nargs = '*') args = parser.parse_args () if args.script == 'a': sub process ('Python a.py% s'% '' .join args .rest_args) and: subprocess.call ('Python b.py% s'% '' .join (args.rest_args))
This works fine, as long as I do what I want to have the argument Is there any way to do this Maybe you are looking for it will parse all the options that recognize it, and - . For example, if I have called
python my_script.py a --foo , then I get an error
unrecognized arguments , when in fact I just call it
python A.py --foo (such as pass with
- foo subprances).
argparse ?
Returns to all unknown arguments in the unknown
import argparse parser = argparse.ArgumentParser () parser.add_argument ('script', '=' ['A', ' B ']) args, unknown = parser.parse_known_args ([' a ',' --foo ']) print (args) #namespace (script =' a ') print (unknown) # [' --foo ']
Comments
Post a Comment