Python Forum
How to pass args to myprog.exe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to pass args to myprog.exe
#2
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.
Recommended Tutorials:
Reply


Messages In This Thread
How to pass args to myprog.exe - by Fran_3 - Sep-04-2017, 12:54 AM
RE: How to pass args to myprog.exe - by metulburr - Sep-04-2017, 02:12 AM
RE: How to pass args to myprog.exe - by Fran_3 - Sep-04-2017, 11:11 PM
RE: How to pass args to myprog.exe - by metulburr - Sep-05-2017, 12:51 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to pass encrypted pass to pyodbc script tester_V 0 1,012 Jul-27-2023, 12:40 AM
Last Post: tester_V
Question How to compare two parameters in a function that has *args? Milan 4 1,536 Mar-26-2023, 07:43 PM
Last Post: Milan
  *args implementation and clarification about tuple status amjass12 10 4,349 Jul-07-2021, 10:29 AM
Last Post: amjass12
  [SOLVED] Good way to handle input args? Winfried 2 2,206 May-18-2021, 07:33 PM
Last Post: Winfried
  Two Questions, *args and //= beginner721 8 3,768 Feb-01-2021, 09:11 AM
Last Post: buran
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,701 Sep-07-2020, 08:02 AM
Last Post: perfringo
  does yield support variable args? Skaperen 0 1,751 Mar-03-2020, 02:44 AM
Last Post: Skaperen
  is there a way: repeat key word args Skaperen 2 2,386 Feb-03-2020, 06:03 PM
Last Post: Skaperen
  Pass by reference vs Pass by value leodavinci1990 1 2,341 Nov-20-2019, 02:05 AM
Last Post: jefsummers
  Using function *args to multiply multiple arguments allusernametaken 8 6,415 Nov-20-2019, 12:01 AM
Last Post: allusernametaken

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020