Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can sys.argv handle optional arguments?

Tags:

from sys import argv script, lira_cbt, [eur_hedge] = argv  if eur_hedge == None:     #Do x else:     #Do y 

I want it to be able to run with just lira_cbt as an argument (doing x), or with both lira_cbt and eur_hedge (doing y). Can this be achieved with sys.argv?

like image 805
Felix Avatar asked Feb 25 '12 07:02

Felix


People also ask

How do you pass an optional argument in Python?

You can define Python function optional arguments by specifying the name of an argument followed by a default value when you declare a function. You can also use the **kwargs method to accept a variable number of arguments in a function.

How do you indicate optional arguments?

To indicate optional arguments, Square brackets are commonly used, and can also be used to group parameters that must be specified together. To indicate required arguments, Angled brackets are commonly used, following the same grouping conventions as square brackets.

How optional arguments can be used within a function?

Functions with optional arguments offer more flexibility in how you can use them. You can call the function with or without the argument, and if there is no argument in the function call, then a default value is used.

What is SYS argv used for?

sys. argv is a list in Python that contains all the command-line arguments passed to the script. It is essential in Python while working with Command Line arguments.


1 Answers

Just use the length of sys.argv

if len(sys.argv) == 2:   # do X else:   # do Y 
like image 198
varunl Avatar answered Sep 22 '22 14:09

varunl