0

How to create an argument in Python ? Assume I have a script install.py that executes packages on a host. I have another config script named config.py where i store the host name & package names. but I want to create my host name as an "argument" that i can run in terminal using this command install . And it should catch the host name as an argument. [i.e 'install linuxServer1' and it should install directly to that host.]

#codes from config.py
hostName = "linuxServer1"

package=['test1',
         'test2'
        ]


#codes from install.py

Install = []

for i in config.package:
 Install.append("deploy:"+i+",host=")

for f in Install:
 subprocess.call(["fabric", f+config.hostName])
2
  • Could you clarify what you are wanting? Do you need python code to get the current computer's hostname? Or python code to execute code on a different (remote) computer? Commented Mar 26, 2015 at 12:06
  • i need python code to execute code on a different (remote) computer. i need to use the remote computer name as an argument not as a variable i used here. Commented Mar 26, 2015 at 12:19

3 Answers 3

1

I think you should have a look on "argparse" module of python, it will solve your current problem :)

Lets take 1 example -

The following code is a Python program that takes a list of integers and produces either the sum or the max:

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))

If asked for help(Assuming the Python code above is saved into a file called prog.py)-

$ python 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)

When run with the appropriate arguments, it prints either the sum or the max of the command-line integers:

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10

If invalid arguments are passed in, it will issue an error:

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'
Sign up to request clarification or add additional context in comments.

4 Comments

|user12@LinuxServer2-16:/bin/toolbox > python test.py 1 2 3 4 Traceback (most recent call last): File "test.py", line 3, in <module> import argparse ImportError: No module named argparse >
Are you using Python 2.6 or earlier?
or may You're using a different version of Python with your script than the one you execute in command line. Make sure that the script is using this interpretor: /usr/lib/python2.7
Try installing argparse: easy_install argparse
1

sys.argv holds all of the command line arguments given to the python command.

import sys

if __name__ == "__main__":
    print(sys.argv) # the list containing all of the command line arguments
    if len(sys.argv) > 1: # The first item is the filename
        host = sys.argv[1]
        print(host)

Comments

0
import argparse
Install = []
hostName = str(sys.argv[1])

for i in config.package:
 Install.append("deploy:"+i+",host=")

for f in Install:
 subprocess.call(["fabric", f+hostName])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.