views:

125

answers:

4
ask_username = True
ask_password = True
ask_message = True
ask_number = True

def Usage():
    print '\t-h, --help:  View help'
    print '\t-u, --username: Username'
    print '\t-p, --password: Password'
    print '\t-n, --number: numbber to send the sms'
    print '\t-m, --message: Message to send'
    sys.exit(1)


opts, args = getopt(sys.argv[1:], 'u:p:m:n:h',["username=","password=","message=","number=","help"])

print opts, args
for o,v in opts:
    if o in ("-h", "--help"):
        Usage()
    elif o in ("-u", "--username"):
        username = v
        ask_username = False
    elif o in ("-p", "--password"):
        passwd = v
        ask_password = False
    elif o in ("-m", "--message"):
        message = v
        ask_message = False
    elif o in ("-n", "--number"):
        number = v
        ask_number = False

#Credentials taken here
if ask_username: username = raw_input("Enter USERNAME: ")
if ask_password: passwd = getpass()
if ask_message: message = raw_input("Enter Message: ")
if ask_number: number = raw_input("Enter Mobile number: ")

I dont think it is, because I am using 4 objects just for checking if command line argument was provided...

Guide me with the best way of doing it..

+16  A: 

You might find that the optparse module is useful -- it allows you to specify which options you want and their types and help text, then to parse the options and get all the results back.

It also auto-generates the help output for you, so you only have to maintain your options in one place.

Andrew Aylett
+1: And nothing else.
S.Lott
argparse is more powerful, IMO. But, optparse is tried and true, and very widely available.
Kevin Little
+1  A: 

argparse is another option if optparse does not cover all your needs

optparse vs argparse

gnibbler
argparse will be added to the standard library in 2.7 and 3.2, BTW.
codeape
+1  A: 

While the right thing is to use optparse, some comments on the code -- as presented -- are in order.

The four condition flags (ask_username, ask_password, ask_message, ask_number) are useless.

Consider your post-condition for any one of these four items (they're all the same)

(in_args AND variable_set) OR (not in_args AND prompt AND variable_set)

You're really doing something like this.

username= None 

# Pre Condition: username is not set to a useful value

# ... use `optparse` to parse options ... 
username = options.username 

# Post Condition: username may be None if no option provided
# or username may be set if an option was provided.

if username is None: # it's not set to a useful value
    # ... prompt to get a value for username ... 

assert username is not None

You don't need any flags when you have default values that are more useful.

S.Lott
This is also an option.. But, i should better use optparse as suggested by others.
shadyabhi
The point is that the core algorithm has too many variables. `optparse` won't eliminate the mis-use of spurious "flags" to duplication conditions that can be described more simply.
S.Lott
+1  A: 

So far optparse has fitted all my needs.

Alastair