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
#1
I've Googled this but it's been a long day and maybe some kind soul will share some simple example code so I can get out of this chair for today :-)

I need simple examples of launching an external .exe file and passing it an argument or arguments?

I think you use subprocess and pOpen but I've yet to find a simple example for say the following...

If run from the command line...

# example 1 - launch a program and pass it some text as an argument
> myprog.exe arg1

# example 2 - launch a program and pass it a file
> vlc test.wav

# example 3 - launch a program and pass it some arguments and get the results
> set /a 2+3
> 5

How to do this from Python?

thanks for any help
Reply
#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
#3
Metulburr,

Thanks for the info on using argparse but I don't think that answers my 3 original questions.

Take a look at the OP if you (or others) don't mind.

(Your response did result in me spending some time understanding argparse and your info along with https://www.youtube.com/watch?v=rnatu3xxVQE more or less got me there... I think :-)

thanks
Reply
#4
Quote:# example 1 - launch a program and pass it some text as an argument
> myprog.exe arg1

# example 2 - launch a program and pass it a file
> vlc test.wav

# example 3 - launch a program and pass it some arguments and get the results
> set /a 2+3
> 5
If you want to start another process such as vlc and give it an argument with python, then you would use the same procedure via argparse. You would just pass the arguments along to the command to start vlc. subprocess.Popen('vlc {}'.format(ARGUMENT))

To evaluate an expression safely without using eval() you could use sympy
example...
>>> import sympy
>>> sympy.sympify('1+2')
3
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to pass encrypted pass to pyodbc script tester_V 0 807 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,198 Mar-26-2023, 07:43 PM
Last Post: Milan
  *args implementation and clarification about tuple status amjass12 10 3,930 Jul-07-2021, 10:29 AM
Last Post: amjass12
  [SOLVED] Good way to handle input args? Winfried 2 2,004 May-18-2021, 07:33 PM
Last Post: Winfried
  Two Questions, *args and //= beginner721 8 3,422 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,513 Sep-07-2020, 08:02 AM
Last Post: perfringo
  does yield support variable args? Skaperen 0 1,643 Mar-03-2020, 02:44 AM
Last Post: Skaperen
  is there a way: repeat key word args Skaperen 2 2,200 Feb-03-2020, 06:03 PM
Last Post: Skaperen
  Pass by reference vs Pass by value leodavinci1990 1 2,165 Nov-20-2019, 02:05 AM
Last Post: jefsummers
  Using function *args to multiply multiple arguments allusernametaken 8 5,951 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