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.

Juan Leni
  • 6,982
  • 5
  • 55
  • 87
TheTank
  • 495
  • 2
  • 9
  • 21
  • 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. – Matt Runion Sep 04 '18 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)`. – hpaulj Sep 05 '18 at 03:10
  • The subparser mechanism can also handle this CLI. Define 'Y' and 'N' subparsers, and give each the required arguments. – hpaulj Sep 05 '18 at 03:16
  • This should give you some ideas: https://stackoverflow.com/questions/52103324/how-to-create-subparser-with-argparse-from-existing-program-in-python-3 – hpaulj Sep 05 '18 at 04:40

1 Answers1

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.

Juan Leni
  • 6,982
  • 5
  • 55
  • 87
  • 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. – TheTank Sep 04 '18 at 20:38
  • You missed a `click` question: https://stackoverflow.com/questions/51874185/command-line-interface-using-click – hpaulj Sep 05 '18 at 04:30