开发者

Is this the right way of handling command line arguments? [closed]

开发者 https://www.devze.com 2022-12-24 00:26 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely solicit debate, a
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 9 years ago.
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_inpu开发者_C百科t("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..


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.


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

optparse vs argparse


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.


So far optparse has fitted all my needs.

0

精彩评论

暂无评论...
验证码 换一张
取 消