You can do this in two ways.
sys module or
argparse module
sys.argv is a list of all arguments given with the first as the name of the script
So the first argument would be the second element via
filename.py
import sys
print(sys.argv[1])
in the command prompt/terminal
python test3.py metul
would result in
Output:
metul
However giving arguments can get complicated. Which is where
argparse comes into play. Its a module to handle complex arguments.
A simple true or false value would be like
import argparse
parser = argparse.ArgumentParser(description='metulburr\'s Arguments')
parser.add_argument('-c','--clean', action='store_true', help='helpful info in the help')
args = vars(parser.parse_args())
print(args)
with the commands and output as...
Output:
metulburr@ubuntu:~$ python test3.py
{'clean': False}
metulburr@ubuntu:~$ python test3.py -c
{'clean': True}
metulburr@ubuntu:~$ python test3.py -h
usage: test3.py [-h] [-c]
metulburr's Arguments
optional arguments:
-h, --help show this help message and exit
-c, --clean helpful info in the help
You can do a lot more than just true/false values. You can do metavars, type, various number of arguments (nargs), easily add/remove arguments, etc. Here is an example of a game we made using this
import argparse
parser = argparse.ArgumentParser(description='Program Arguments')
parser.add_argument('-c','--center', action='store_false',
help='position starting window at (0,0), sets SDL_VIDEO_CENTERED to false')
parser.add_argument('-w','--winpos', nargs=2, default=(0,0), metavar=('X', 'Y'),
help='position starting window at (X,Y), default is (0,0)')
parser.add_argument('-s' , '--size', nargs=2, default=(800,600), metavar=('WIDTH', 'HEIGHT'),
help='set window size to WIDTH HEIGHT, defualt is {}'.format((800,600)))
parser.add_argument('-f' , '--fullscreen', action='store_true',
help='start in fullscreen')
parser.add_argument('-m' , '--music_off', action='store_true',
help='start with no music')
parser.add_argument('-S', '--straight', action='store', type=str,
help='go straight to the named game')
parser.add_argument('-M', '--money', default=100, metavar='VALUE',
help='set money to value')
parser.add_argument('-d', '--debug', action='store_true',
help='run game in debug mode')
parser.add_argument('-F', '--FPS', action='store_true',
help='show FPS in title bar')
parser.add_argument('-p', '--profile', action='store_true',
help='run game with profiling')
parser.add_argument('-B', '--bots', action='store_true',
help='enable test bots')
parser.add_argument('-N', '--iterations', action='store', type=int,
help='maximum number of iterations to run for (useful with profiling option')
args = vars(parser.parse_args())
print(args)
Output:
metulburr@ubuntu:~$ python test3.py -m
{'profile': False, 'fullscreen': False, 'iterations': None, 'center': True, 'money': 100, 'straight': None, 'FPS': False, 'debug': False, 'bots': False, 'music_off': True, 'winpos': (0, 0), 'size': (800, 600)}
metulburr@ubuntu:~$ python test3.py -md
{'profile': False, 'fullscreen': False, 'iterations': None, 'center': True, 'money': 100, 'straight': None, 'FPS': False, 'debug': True, 'bots': False, 'music_off': True, 'winpos': (0, 0), 'size': (800, 600)}
metulburr@ubuntu:~$ python test3.py -w 100 100
{'profile': False, 'fullscreen': False, 'iterations': None, 'center': True, 'money': 100, 'straight': None, 'FPS': False, 'debug': False, 'bots': False, 'music_off': False, 'winpos': ['100', '100'], 'size': (800, 600)}
metulburr@ubuntu:~$ python test3.py -md -w 100 100
{'profile': False, 'fullscreen': False, 'iterations': None, 'center': True, 'money': 100, 'straight': None, 'FPS': False, 'debug': True, 'bots': False, 'music_off': True, 'winpos': ['100', '100'], 'size': (800, 600)}
metulburr@ubuntu:~$
As you can see you would have to add a lot of string manipulation to handle what argparse does if you used sys.argv. It also doesnt matter regarding the file. If you compiled your py file into an exe then you would just change the extension and path to the exe with the same args.