2

Here is my sample code:

def function1():

    parser = argparse.ArgumentParser(description='Test Cascading Utility')
    parser.add_argument('--number', type=str, help='Enter number')
    args = parser.parse_args()
    x = str(args.number)
    squares = float(x)**2

def function2():

    parser = argparse.ArgumentParser(description='Test Cascading Utility')
    parser.add_argument('--number1', type=str, help='Enter number')
    parser.add_argument('--number2', type=str, help='Enter number')
    args = parser.parse_args()
    x = str(args.number1)
    y = str(args.number2)
    div = float(x)/float(y)

def main():
    choice = sys.argv[1]
    if choice == 'Y':
        function1()
    elif choice == 'N':
        function2()
    else:
        print("Come on, choose a Y or N option.")

if __name__ == '__main__':
    main()

I am trying to create a cascading cli tool where based on one option I enter, it runs a particular method. This method will in turn have its own set of arguments. This particular code throws an error: error: unrecognized arguments: Y This leads me to think 'choice' system argument is being overridden by the argument parser, so how can I implement this cascading effect where based on the choice I run the method.

This is my first time delving into argparse and hence please bear with me if the question is silly. But it is something I really would like to implement.

4
  • ArgumentParser is made to parse arguments that are passed to the application when it is ran. You only get those arguments when the applications starts. You can't create separate ArgParsers in separate functions like that and expect them to have values. Commented Sep 4, 2018 at 20:37
  • parse_args looks at sys.argv[1:]. Your choice is in that list. You either have to remove it before calling the function, or write the parser so it doesn't choke on that string. Another possibility is to pass a argv list to the function and use parse_args(argv). Commented Sep 5, 2018 at 3:10
  • The subparser mechanism can also handle this CLI. Define 'Y' and 'N' subparsers, and give each the required arguments. Commented Sep 5, 2018 at 3:16
  • This should give you some ideas: stackoverflow.com/questions/52103324/… Commented Sep 5, 2018 at 4:40

1 Answer 1

0

I would recommend you to use click. It makes these things very simple

http://click.pocoo.org/5/

You need to use groups and maybe multicommand chaining

http://click.pocoo.org/5/commands/#group-invocation-without-command http://click.pocoo.org/5/commands/#multi-command-chaining

You can create groups and subcommands. Then in each subcommand call the original functions that you are integrating with.

Sign up to request clarification or add additional context in comments.

2 Comments

I can't really change the functions since I am going to be integrating someone else's code into mine. I do not want to make changes to the functions themselves. I don't think Click can help me achieve that, if it can, i would like to know how since I have used click before and I have not been able to implement the cascading effect.
You missed a click question: stackoverflow.com/questions/51874185/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.