0

I have been having trouble using argv in the module sys in python, I would like to have a script that takes options like a program like vim or another program that takes arguments like -t or something like that. in those kinds of programs you have have one argument or more. If i want one argument it gives me a value error, is there a way to have one option or more using argv? if not how do programs like ls or vim use options?

import sys

argv = sys.argv

script, option1, option2, option3 = argv
if option1 or option2 or option3 == '--help':
    print 'Help will be added after the script is finished W-I-P'
elif option1 or option2 or option3 == '':
    print 'if you need help type time.py --help'
else:
    print 'Syntax error refer to --help for options'

thanks!

PizzaCat
  • 13
  • 5
  • 4
    You should look into the `argparse` module. – cs95 Jul 06 '17 at 00:26
  • 1
    Also, your if statements don't do what you want, because of how you've written them. – cs95 Jul 06 '17 at 00:26
  • can echo @cᴏʟᴅsᴘᴇᴇᴅ argparse is designed to handle this – PYA Jul 06 '17 at 00:28
  • 1
    [this SO question](https://stackoverflow.com/questions/7427101/dead-simple-argparse-example-wanted-1-argument-3-results) was a good starting point for me on how to use `argparse` – patrick Jul 06 '17 at 00:32
  • By all means, learn `argparse`. In the meantime, think in the other direction: `if "--help" in argv`. If you already have the arguments handy in a list, why put them into different variables with generic names? Use `argparse` with this concept, and you'll have much better code. – Prune Jul 06 '17 at 00:33
  • @cᴏʟᴅsᴘᴇᴇᴅ Is right. When you start needing that kind of functionality, a premade argument parsing library is the correct way to go. There are plenty out there. Personally, I've had good experience with [`argparse`](https://docs.python.org/3.6/howto/argparse.html). But there are others such as [click](http://click.pocoo.org/5/). – Christian Dean Jul 06 '17 at 00:33
  • I agree that `argparse` is the way to go here. But to make your particular case work, you could provide default values for the options by adding this line just before the `script, ...` assignment: `argv = argv + [''] * (4-len(argv))`. – Matthias Fripp Jul 06 '17 at 00:51
  • @MatthiasFripp The default assignment thing was what i was looking for, thanks – PizzaCat Jul 06 '17 at 01:02
  • I will look into argparse – PizzaCat Jul 06 '17 at 01:03
  • `c` programs often use a `getopt` library. Python has a module of that name too. `argparse` is the current recommend Python parser, but there are others on `pypi` that try to improve on it in various ways. If you have questions on `argparse` first do a search with its `tag`. – hpaulj Jul 06 '17 at 01:53

1 Answers1

0

Does this help?

import sys

argv = sys.argv

if '--help' in argv:
    print 'Help will be added after the script is finished W-I-P'

elif len(argv) < 4:
    print 'Syntax error refer to --help for options'

else:
    script, option1, option2, option3 = argv

Output:

> python test.py --help
Help will be added after the script is finished W-I-P

> python test.py 1 2 3 --help
Help will be added after the script is finished W-I-P

> python test.py 1 2
Syntax error refer to --help for options

> python test.py 1 2 3
> 
s4w3d0ff
  • 1,011
  • 10
  • 24