Passing a numeric command line argument to a script

This is the place for queries that don't fit in any of the other categories.

Passing a numeric command line argument to a script

Postby Leo » Wed Nov 06, 2013 9:13 pm

Hi

I'm new to python, and am embarrassed to ask this python question, since it's so trivial. But I've been trawling through a mass of online tutorials , and they all seem to focus on different things

I have the following python script, called setSound.py

Code: Select all
#!/usr/bin/python
import sys
import alsaaudio

print 'Sound volume is being set to', str(sys.argv)
m = alsaaudio.Mixer()   # defined alsaaudio.Mixer to change volume
m.setvolume( ARG1 ) # set volume
vol = m.getvolume() # get volume float value



All I need to do is pass the value ARG1 to the script as a command line argument, i.e., I want to be able to type setSound.py 50 and have it replace ARG1 by 50.

If anybody could explain, I'd be most grateful.
Last edited by micseydel on Wed Nov 06, 2013 9:18 pm, edited 1 time in total.
Reason: First post lock.
Leo
 
Posts: 2
Joined: Wed Nov 06, 2013 8:35 pm

Re: Passing a numeric command line argument to a script

Postby metulburr » Wed Nov 06, 2013 9:42 pm

assuming you are not going to have any error checking in regards to if the user did not put an argument to the program, or put a string rather than an integer:

just replace this
Code: Select all
m.setvolume( ARG1 )

to this
Code: Select all
m.setvolume(int(sys.argv[1]))


Code: Select all
vol = m.getvolume() # get volume float value

however if it has to be of type float, use float() instead of int()
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1471
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Passing a numeric command line argument to a script

Postby Leo » Wed Nov 06, 2013 10:46 pm

Thanks so much. I appreciate the help
Leo
 
Posts: 2
Joined: Wed Nov 06, 2013 8:35 pm


Return to General Coding Help

Who is online

Users browsing this forum: snippsat and 5 guests