0

Hi is anyone able to help. I am learning to use argparse and i want to use the command to call the school.py as school start for example. I have this so far but struggling to handle the arguments. Am i doing this right or what am i doing totally wrong?

if __name__ == '__main__':


parser = argparse.ArgumentParser(description="This allows quick opening of applications used within the school day")

parser.add_argument("start", help="This will open all the standard applications used within the school day.")
parser.add_argument("engine", help="This will show the Engineering folder within Documents")
parser.add_argument("bus", help="This will show the Business folder within Documents")
parser.add_argument("cs", help="This will show the Computer Science folder within Documents")
parser.add_argument("python", help="This will open the PyCharm application")

args = parser.parse_args()

try:
    if len(sys.argv) > 1:
        if sys.argv[1] == "engine":
            engineering()
        elif sys.argv[1] == "cs":
            computer_science()
        elif sys.argv[1] == "python":
            python()
        elif sys.argv[1] == "bus":
            business()
        elif sys.argv[1] == "start":
            std_day()
except:
    print("An error has occurred")

My error is

usage: autoSchoolDay.py [-h] start engine bus cs python

autoSchoolDay.py: error: the following arguments are required: engine, bus, cs, python

12
  • you are adding multiple arguments called "start", "engine", etc. when you need to add a single argument which can be one of "start", "engine", etc. like this: parser.add_argument('command', choices=['start', 'engine', 'bus', 'cs', 'python'], default='start') and then you'd do if args.command == "engine": and so on. Commented Mar 1, 2021 at 20:48
  • Yes i want to call it like school start or school engine Commented Mar 1, 2021 at 20:50
  • How are you calling this script? From a OS shell? WIth what commandline arguments? Or do you even know what we mean by command line arguments? Commented Mar 1, 2021 at 20:50
  • @JamesMcC please try the code I added to my comment, I understand what you want. Commented Mar 1, 2021 at 20:51
  • 1
    If using argparse you shouldn't need to parse sys.argv directly. However, the logic defined for your parser is different from your handling of sys.argv. Commented Mar 1, 2021 at 20:53

1 Answer 1

1
parser = argparse.ArgumentParser(description="This allows quick opening of applications used within the school day")

parser.add_argument('command', choices=['start', 'engine', 'bus', 'cs', 'python'])

args = parser.parse_args()

try:
    if args.command:
        if args.command == "engine":
            engineering()
        elif args.command == "cs":
            computer_science()
        elif args.command == "python":
            python()
        elif args.command == "bus":
            business()
        elif args.command == "start":
            std_day()
except Exception as e:
    print("An error has occurred", e)
Sign up to request clarification or add additional context in comments.

2 Comments

This works and now i get it. Question though. How can i make the help section now if all the commands are combined into one
You are not the first person who has wanted to do what you want to do. You can find other people asking about this on stack overflow or tutorials on the internet, the keyword is "subcommand". I also linked you to the documentation for what you should try using in a comment.

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.