Welcome to Dream.In.Code
Getting Help is Easy!

Join 118,659 Programmers for FREE! Ask your question and get quick answers from experts. There are 863 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



using sys.argv to execute on a numeric range.

 
Reply to this topicStart new topic

using sys.argv to execute on a numeric range.

cronos4d
post 30 Jun, 2008 - 06:44 PM
Post #1


New D.I.C Head

*
Joined: 16 May, 2008
Posts: 9

I have the following code
CODE
if sys.argv[4] == 'state':
    try:

        print(sys.argv[5])

    except:

        print "Error gathering server state ", sys.argv[5]


I invoke the python program with myscript.py option1, 2, 3, etc

The fifth argument is a server with a numeric range:
server1,server2,server3, etc.

Instead of wrapping the script, I would like to be able to feed a range to the script instead.
For example, right now all I can do is myscript.py server1, myscript.py server2

I would like to be able to pass myscript.py server1-20 and it would execute the command on all 20 servers.

Any suggestions are appreciated.
User is offlineProfile CardPM

Go to the top of the page


linuxunil
post 1 Jul, 2008 - 07:33 AM
Post #2


New D.I.C Head

Group Icon
Joined: 7 Mar, 2006
Posts: 46



Thanked 2 times

Dream Kudos: 125
My Contributions


It would probably be easier and more useful to just put the servers in a configuration file and load it. This would keep you from having to type the server over and over again. You could even use a YAML file and declare groups of servers. Take a look at PyYaml. YAML is much easier to read and write than XML.
User is offlineProfile CardPM

Go to the top of the page

cronos4d
post 1 Jul, 2008 - 12:42 PM
Post #3


New D.I.C Head

*
Joined: 16 May, 2008
Posts: 9

QUOTE(linuxunil @ 1 Jul, 2008 - 07:33 AM) *

It would probably be easier and more useful to just put the servers in a configuration file and load it. This would keep you from having to type the server over and over again. You could even use a YAML file and declare groups of servers. Take a look at PyYaml. YAML is much easier to read and write than XML.


I agree that this might be easier; however, I do not always want to execute commands against all servers in the config file. Hence the need for a range.
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 12 Jul, 2008 - 08:22 AM
Post #4


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 550



Thanked 43 times

Dream Kudos: 25
My Contributions


QUOTE(cronos4d @ 30 Jun, 2008 - 06:44 PM) *

I have the following code
CODE
if sys.argv[4] == 'state':
    try:

        print(sys.argv[5])

    except:

        print "Error gathering server state ", sys.argv[5]


I invoke the python program with myscript.py option1, 2, 3, etc

The fifth argument is a server with a numeric range:
server1,server2,server3, etc.

Instead of wrapping the script, I would like to be able to feed a range to the script instead.
For example, right now all I can do is myscript.py server1, myscript.py server2

I would like to be able to pass myscript.py server1-20 and it would execute the command on all 20 servers.

Any suggestions are appreciated.


Use ZSH. smile.gif
Actually, bash supports this syntax also.

myscript.py server{1..20}

Expands to:
myscript.py server1 server2 server3 ... server20

Then simply process the argument list. If you have other arguments besides servers that get passed to your program, then make them options (as in, begin with one or two dashes... --long-option). You can also implement an option terminator option (just two dashes, --) to indicate that the rest of the arguments in the list are not options.

User is offlineProfile CardPM

Go to the top of the page

linuxunil
post 14 Jul, 2008 - 07:28 AM
Post #5


New D.I.C Head

Group Icon
Joined: 7 Mar, 2006
Posts: 46



Thanked 2 times

Dream Kudos: 125
My Contributions


You may be better off with optparse, it provides more flexibility in setting up you option parsing. Reference Guide
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/12/08 03:57AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month