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.
parse_argslooks atsys.argv[1:]. Yourchoiceis 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 aargvlist to the function and useparse_args(argv).