1

I'm creating a script to automate plethora of related tasks, however I'd like to be able to run specific parts of the script from command line, such as C:\> python main.py --debug (or just -d) Or something like C:\> python main.py --log (or just -l)

I've tried a lot of websites and videos but I haven't been able to find a solution. I've read the python docs but I don't really understand it very much yet as I'm very very beginner level. I don't want to have to run python main.py -l True or -l Yes or anything, I just want to be able to set a boolean to True if -l is present.

I'm using arparse and I just want to be able to say, if the script is run with argument "-d", then set variable x to True Similarly, if script is run with argument "-l" then it would set y to True

I have been able to set up descriptions

import argparse

myparser = argparse.ArgumentParser(description='Script for logging and analyzing player data')
myparser.add_argument('--debug', '-d', required=False, action='debug_mode', help='Run in DEBUG Mode')
myparser.add_argument('--log', '-l', required=False, action='logger', help='Only run loggers')
myparser.add_argument('--autorun', '-a' required=False, action='enable_autorun', help='Logs data every day')

I don't want to require the arguments either, I want them to be optional.

I'm very new to python and I genuinely don't understand this argparse module, any help would be greatly appreciated :))

itzilly
  • 11
  • 2
  • I suggest reading the official docs. They are a pretty good tutorial. Most of the others I've seen online are frankly crap. – Mad Physicist Jan 08 '22 at 06:47
  • Those `action` strings are not valid. If you want True/False` values use 'store_true'. Read the docs. `argparse` tagged SO answers might slso help you get started. Some are advanced, but many help novices get going. – hpaulj Jan 08 '22 at 07:10
  • In the duplicate, use the newer answer by @blhsing. The longer accepted answer deals with `-foo True` types of inputs, which you don't need. – hpaulj Jan 08 '22 at 07:39

0 Answers0