58

I can call my script like this:

python D:\myscript.py 60

And in the script I can do:

arg = sys.argv[1]
foo(arg)

But how could I test if the argument has been entered in the command line call? I need to do something like this:

if isset(sys.argv[1]):
    foo(sys.argv[1])
else:
    print "You must set argument!!!"
Richard Knop
  • 81,041
  • 149
  • 392
  • 552
  • check if argv[1] is null, sorry i thought it was implicit that I was talking about argument 1. – Jim Nov 15 '10 at 20:27
  • 1
    @Jim `sys.argv` is not null, it contains at least the script's name as `sys.argv[0]` – khachik Nov 15 '10 at 20:29

9 Answers9

71
import sys
len( sys.argv ) > 1
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
khachik
  • 28,112
  • 9
  • 59
  • 94
64

Don't use sys.argv for handling the command-line interface; there's a module to do that: argparse.

You can mark an argument as required by passing required=True to add_argument.

import argparse
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument("foo", ..., required=True)
parser.parse_args()
Katriel
  • 120,462
  • 19
  • 136
  • 170
  • 19
    If needs are very simple, is there actually something *wrong* with using `sys.argv`? – Jonik Nov 15 '10 at 20:30
  • 1
    @Jonik, not necessarily, but there's very rarely any reason _not_ to use it. Just as regexes are _very occasionally_ useful for parsing HTML, but there's very rarely any reason not to use an HTML parser. – Katriel Nov 15 '10 at 20:32
  • 4
    -1 for providing an example that would not work in Richard's situation. If `foo` is `60` then it will be a positional and you cannot add `required=True` to a positional. – geedoubleya Mar 22 '17 at 16:12
30
if len(sys.argv) < 2:
    print "You must set argument!!!"
chris
  • 3,986
  • 1
  • 26
  • 32
8
if len(sys.argv) == 1:
   print('no arguments passed')
   sys.exit()

This will check if any arguments were passed at all. If there are no arguments, it will exit the script, without running the rest of it.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Miguel Armenta
  • 231
  • 3
  • 4
  • Thanks for your answer! Note that there are already other answers that are very similar to this. – bohrax Aug 14 '18 at 19:06
6

If you're using Python 2.7/3.2, use the argparse module. Otherwise, use the optparse module. The module takes care of parsing the command-line, and you can check whether the number of positional arguments matches what you expect.

DNS
  • 37,249
  • 18
  • 95
  • 132
2
for arg in sys.argv:
    print (arg)  
    #print cli arguments

You can use it to store the argument in list and used them. Is more safe way than to used them like this sys.argv[n]

No problems if no arguments are given

Pat
  • 1,173
  • 5
  • 19
  • 44
2

This script uses the IndexError exception:

try:
    print(sys.argv[1])
except IndexError:
    print("Empty argument")
Enrique Pérez Herrero
  • 3,699
  • 2
  • 32
  • 33
0

I use optparse module for this but I guess because i am using 2.5 you can use argparse as Alex suggested if you are using 2.7 or greater

Rafi
  • 805
  • 6
  • 12
-10

if(sys.argv[1]): should work fine, if there are no arguments sys.argv[1] will be (should be) null

J V
  • 11,402
  • 10
  • 52
  • 72