2

Possible Duplicate:
Command Line Arguments In Python

Example:

itp.threads[0].memload(r"c:\FistForSOCAD_PASS.bin","0x108000P")
itp.threads[0].cv.cip = "0x108000"
itp.go()
itp.halt()

I would like to make above example as a python script and "FistForSOCAD_PASS.bin" can be replaced by an argument. Because it is only the variable thing.

Example command to run the script will be like:

python itp.py FistForSOCAD_PASS.bin

Community
  • 1
  • 1
SeiZai
  • 35
  • 3

3 Answers3

6

There are a number of ways, One is this approach:

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
                   help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
                   const=sum, default=max,
                   help='sum the integers (default: find the max)')

args = parser.parse_args()
print args.accumulate(args.integers)

Which gives:

$ prog.py -h
usage: prog.py [-h] [--sum] N [N ...]

Process some integers.

positional arguments:
 N           an integer for the accumulator

optional arguments:
 -h, --help  show this help message and exit
 --sum       sum the integers (default: find the max)

Read more at docs.python.org

Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
4

You may use sys.argv:

sys.argv

The list of command line arguments passed to a Python script. argv[0] is the script name (it is operating system dependent whether this is a full pathname or not). [...]

Example:

import sys
if len(sys.argv) > 1:
    filename = sys.argv[1]
Community
  • 1
  • 1
moooeeeep
  • 31,622
  • 22
  • 98
  • 187
0

You might want to look at the argparse module.

Neil
  • 54,642
  • 8
  • 60
  • 72
Aesthete
  • 18,622
  • 6
  • 36
  • 45