1

I need to create an "interface" to my script such that (runs by crontab):

  1. When --help, terminal will display a list of choices (well formatted, separate by \n)
  2. allow multiple choices input (comma delimited)

e.g. (something like the following)

python feedAnimals.py --help 
...... Choices:
           dog
           cat
           fish

python feedAnimals.py --pets dog,cat,fish

Is there anyway to do this with type="choices"? Or can I use type="string"? I tried to insert \n between choices under the "help" option, but these seems to be neglected at run time.

Must be python 2.4 compatible :(

eddy85br
  • 284
  • 4
  • 12
user3388884
  • 4,748
  • 9
  • 25
  • 34
  • you want to add option for your script? or wish to create interactive area? like : select your option: 1)foo 2)bar and then user will select one of them. ? – mortymacs Jun 19 '14 at 17:00
  • 1
    it'll need to run on cronjob... so can't be interactive :( – user3388884 Jun 19 '14 at 19:41
  • 1
    I didn't got you completely. you wanna get `argv` in your script in `python 2.4`? so you didn't test `sys.argv` ? – mortymacs Jun 19 '14 at 20:06
  • In the OptionParser code where I specify the details of --help, I need to add in new lines... will this be possible? – user3388884 Jun 19 '14 at 20:11
  • new line for content of help? you means something like it: "Choices:\n\tdog\n\tcat\n\tfish" ? – mortymacs Jun 19 '14 at 20:46

3 Answers3

1

Try looking at the documentation for argparse, should do what you need - and help (-h, --help) is built in by default

https://docs.python.org/2/library/argparse.html

gkusner
  • 1,244
  • 1
  • 11
  • 14
  • Need to be 2.4 compatible :( – user3388884 Jun 19 '14 at 19:44
  • You can install it for versions prior to 2.7 - google it – gkusner Jun 19 '14 at 20:10
  • @user3388884 No you don't... and even if you do, as noted by gkusner, you can install `argparse` as a third party module. See [the PyPI page.](https://pypi.python.org/pypi/argparse), in fact `argparse` can run from python2.3 onwards. – Bakuriu Nov 02 '16 at 10:00
1

This is example of how to change usage value. Try it:

from optparse import OptionParser 
string = "Choices:\n\tdog\n\tcat\n\tfish"
parser = OptionParser(usage=string)
(options,args) = parser.parse_args()

You can also change your string to this style:

string = """
    Choices:
        dog
        cat
        fish
"""

Then test it:

$python code.py --help

In will show you this something like this result:

Usage: 
    Choices:
        dog
        cat
        fish


Options:
  -h, --help  show this help message and exit
mortymacs
  • 3,456
  • 3
  • 27
  • 53
1

Look this related questions, the first with a good "type='choice'" example, and the second with multiple values:

Set a default choice for optionparser when the option is given

Processing multiple values for one single option using getopt/optparse?

You may use something like this or process the arguments "by hand":

from optparse import OptionParser

def get_args():
  usage = "Usage: %prog [options]"

  parser = OptionParser()

  parser.add_option("--pet",
    type = "choice",
    action = 'append',
    choices = ["dog", "cat", "fish"],
    default = [],
    dest = pets,
    help = "Available pets: [dog, cat, fish]"
  )

  (options, args) = parser.parse_args()

  print options, args
  return (options, args)

(opt, args) = get_args()
print opt.pets

Then, run:

python test.py --pet cat --pet dog --pet fish
Community
  • 1
  • 1
eddy85br
  • 284
  • 4
  • 12
  • The question is a little bit different. I've tried to comment, but I don't have enough reputation and now there is a link to that similar question. Thanks for the tips, next time I'll elaborate a better answer! – eddy85br Nov 16 '16 at 20:07
  • I've improved the answer, with sample code, adapted and shortened version from the other question. – eddy85br Nov 16 '16 at 20:17
  • Other related question: http://stackoverflow.com/questions/7671383/creating-an-array-from-a-command-line-option-pythonoptparse – eddy85br Nov 16 '16 at 20:36