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 - . 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).

Is there any way to do this argparse ?

Maybe you are looking for it will parse all the options that recognize it, and 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

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -