6

when I accept arguments how do I check if two show up at the same time without having a compound conditional

i.e.

#!/usr/bin/python
import random, string
import mymodule
import sys

z = ' '.join(sys.argv[2:])
q = ''.join(sys.argv[3:])
a = ''.join(sys.argv[2:])
s = ' '.join(sys.argv[1:])
flags = sys.argv[1:5]

commands = [["-r", "reverse string passed next with no quotes needed."], ["-j", "joins arguments passed into string. no quotes needed."], ["--palindrome", "tests whether arguments passed are palindrome or not. collective."],["--rand","passes random string of 10 digits/letters"]]

try:
    if "-r" in flags:
        if "-j" in flags:
            print mymodule.reverse(q)
        if not "-j" in flags:
            print mymodule.reverse(z)

    if "-j" in flags:
        if not "-r" in flags:
            print a

    if "--palindrome" in flags: mymodule.ispalindrome(z)

    if (not "-r" or not "-j" or not "--palindrome") in flags: mymodule.say(s)

    if "--rand" in flags: print(''.join([random.choice(string.ascii_letters+"123456789") for f in range(10)]))

    if not sys.argv[1]: print mymodule.no_arg_error

    if "--help" in flags: print commands

except: print mymodule.no_arg_error

i just want to be able to say

if "-r" and "-j" in flags in no particular order: do whatever

0

4 Answers 4

7

Something like

import optparse

p = optparse.OptionParser()
p.add_option('--foo', '-f', default="yadda")
p.add_option('--bar', '-b')
options, arguments = p.parse_args()

# if options.foo and options.bar ...
6
  • what does "default" do? just curious how I would incorporate that
    – tekknolagi
    Jan 9, 2011 at 23:11
  • new to python, not programming
    – tekknolagi
    Jan 9, 2011 at 23:12
  • and optparse seems to be deprecated so i'll go with getopt?
    – tekknolagi
    Jan 9, 2011 at 23:12
  • 1
    p.add_option('--year', '-y', default='2011'). -> blah.py --year 2010.
    – Bjorn
    Jan 9, 2011 at 23:13
  • optparse is deprecated, but argparse is only introduced in 2.7. OS X, for example, runs 2.6 by default. If you are sure your audience is not on 2.6 anymore, you should go with argparse. Otherwise I'd recommend to stay with optparse, because it'll work on both 2.6 and 2.7.
    – Bjorn
    Jan 9, 2011 at 23:16
4

I'd recommend using argparse for this (or optparse if you're on Python 2.6.x or older).

Without a module you'd do this:

if "-r" in flags and "-j" in flags:
    do whatever

But I suggest you read the documentation for argparse and learn how to use it. You will be happy you did.

1
  • i am unfamiliar with optparse and that's a reason.
    – tekknolagi
    Jan 9, 2011 at 23:07
0

Also see getopt. It has a bit more terse syntax, and a complete example in docs.

2
  • what do you mean by terse syntax?
    – tekknolagi
    Jan 9, 2011 at 23:06
  • With optparse, you generally add_option() for each option. With getopt, you just write something like options, fnames = getopt("abf:") and this handles -a, -b and -f filename.
    – 9000
    Jan 10, 2011 at 1:31
0

'or' only can identify one,u should use 'a or b and c'

if (not "-r" or not "-j" and not "--palindrome") in flags: mymodule.say(s)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.