What you're looking for is either the sys module, or the optparse module.
sys will give you very basic control over command line args.
For example:
import sys
if __name__ == "__main__":
if len[sys.argv]>1:
print sys.argv[1]
In the above example, if you were to open up a shell and type -
test.exe "myname"
The resultant output would be:
myname
Note that sys.argv[0] is the name of the script you are currently running. Each subsequent argument is defined by a space, so in your example above
test.exe -- myname
argv[0] = "test.exe"
argv[1] = "--"
argv[2] = "myname"
Optparse gives a much more robust solution that allows you to define command line switches with multiple options and defines variables that will store the appropriate options that can be accessed at runtime.
Re-writing your example:
from optparse import OptionParser
def makeFile[options = None]:
if options:
f = open['mytext.txt', 'w'] #create text file in local dir
print >> f, 'hello, ' + options.name + '! \nHow are ya?'
f.close[]
else:
f = open['mytext.txt', 'w'] #create text file in local dir
print >> f, 'hello, person! \nHow are ya?'
f.close[]
if __name__ == "__main__":
parser = OptionParser[]
parser.add_option['-n','--name',dest = 'name',
help='username to be printed out']
[options,args] = parser.parse_args[]
makeFile[options]
You would run your program with :
test.exe -n myname
and the output [in myfile.txt] would be the expected:
Hello, myname!
How are ya?
Hope that helps!
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:
[email protected]:~$ python test3.py
{'clean': False}
[email protected]:~$ python test3.py -c
{'clean': True}
[email protected]:~$ 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:
[email protected]:~$ 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]}
[email protected]:~$ 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]}
[email protected]:~$ 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]}
[email protected]:~$ 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]}
[email protected]:~$
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.