2 Replies - 2621 Views - Last Post: 29 August 2011 - 06:35 PM Rate Topic: -----

#1 monster92  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 10-May 11

Problem Explaining 'argv'

Posted 25 July 2011 - 02:24 PM

Hello

Please can someone take some time to read my comments in the below script. Now, I understand how I can use argv. However, I'm having trouble explaining how it actually works. I think it's because there's gaps in my terminology. I was wondering whether someone can tell me if the comments are correct ans show examples of how I could of improved my description. Yes, I'm a n00b. Thank you for your time.








from sys import argv # argument variable allows python to unpack variables.

script, user_name = argv # the user will need to type a variable for the script to run
prompt = '>'

print "Hi %s, I'm the %s script." % (user_name, script)
print "I'd like to ask you a few questions"
print "Do you like me %s" % user_name
likes= raw_input(prompt)

print "Where do you live %s?" % user_name
lives = raw_input(prompt)

print "What kind of computer do you have?"
computer = raw_input(prompt)

print """
Alright so you said %r about liking me.
You live in %r. Not sure where that is.
And you have a %r computer. Nice
""" % (likes, lives, computer)



Is This A Good Question/Topic? 0
  • +

Replies To: Problem Explaining 'argv'

#2 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Problem Explaining 'argv'

Posted 25 July 2011 - 02:51 PM

Looks like someone is learning the hard way :P Nice.

Personally, this is what would satisfy me:

# Import the argv module so that we can get 
# arguments from the command line.
from sys import argv

# script = argv[0], user_name = argv[1]
# argv[0] is the name of the script and 
# argv[1] is the first argument given,
# in this case, the user's name.
script, user_name = argv
prompt = '> '

print "Hi %s, I'm the %s script." % (user_name, script)
print "I'd like to ask you a few questions."
print "Do you like me %s?" % user_name
likes = raw_input(prompt)

print "Where do you live %s?" % user_name
lives = raw_input(prompt)

print "What kind of computer do you have?"
computer = raw_input(prompt)

print """
Alright so you said %r about liking me.
You live in %r. Not sure where that is.
And you have a %r computer. Nice.
""" % (likes, lives, computer)


Everything below the variable declarations is self-explanatory, so don't waste your time commenting it.
Was This Post Helpful? 1
  • +
  • -

#3 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Problem Explaining 'argv'

Posted 29 August 2011 - 06:35 PM

Here is how argv works... more or less...


Fake Console#!/!#:mount -t ntfs-3g /dev/sda1 /mnt
--------------Argv is a list!----------------
['mount', '-t', 'ntfs-3g', '/dev/sda1', '/mnt'] is <type 'list'>
--------------Argv is a list!----------------
-------------Here are its elements-----------
mount
-t
ntfs-3g
/dev/sda1
/mnt
-------------Here are its elements-----------
------------This is how to parse them in 30 seconds of code---------------
Mounted  /dev/sda1  to  /mnt  as type ntfs-3g
------------This is how to parse them in 30 seconds of code---------------






cmdline = raw_input("Fake Console#!/!#:")
argv = cmdline.split()

print "--------------Argv is a list!----------------"
print str(argv) + " is " + str(type(argv))
print "--------------Argv is a list!----------------"


print "-------------Here are its elements-----------"
for arg in argv:
    print arg
print "-------------Here are its elements-----------"

    
print "------------This is how to parse them in 30 seconds worth of code---------------"  
type_arg = None   
for arg in argv:
    if "-t" in arg:
        type_arg = argv[argv.index('-t') + 1]
    elif len(argv) < 3:
        print "Usage: mount [-t <type>] source destination"
    else:
        dest = argv[-1]
        source = argv[-2]

        
if(type_arg):
    print "Mounted ", source, " to ", dest, " as type " + type_arg
else:
    print "Mounted ", source, " to " , dest
print "------------This is how to parse them in 30 seconds of code---------------"  



This post has been edited by Python_4_President: 29 August 2011 - 06:39 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1