I am trying to create a command line tool (letβs call it βXβ) that wraps another tool (let's call it βYβ).
I handle some cases on purpose and add some parameters for myself, but I want to redirect what I donβt want to handle the Y tool.
So far, I have managed to redirect arguments that don't come with dashes, for example XY option1 option2 option3 will just call Y option1 option2 option3 . I did this by adding a subparameter Y to it and the argument any
Here's the code (x.py):
main_parser = argparse.ArgumentParser() subparsers = main_parser.add_subparsers(dest="parser_name") y_subparser = subparsers.add_parser('y') y_options = y_subparser.add_argument('any', nargs='*')
Then in my handler code, I do this:
args = main_parser.parse_args() if args.parser_name == 'y': command_string = ' '.join(['y'] + sys.argv[2:]) os.system(command_string)
When I call python x.py y asdf zxcv qwer , it works.
When I call python x.py y asdf zxcv qwer -option , I get the x.py: error: unrecognized arguments: -option
I understand that if the material just gets too complicated with argparse, I can always revert to using sys.argv , but if you know this is doable, share it.
I am also looking at argparse code, which is a little tight, and where it seems that ArgumentParser._parse_known_args does everything (300 lines). But before I go deeper, I thought that maybe someone knows how to do this - if not, I will publish my findings here if someone has another problem.