Home > database >  Can i pass a function as an argument on the command line on Python?
Can i pass a function as an argument on the command line on Python?

Time:01-14

So we have a python code that we execute on the command line with some arguments py code.py -p1 a -p2 b. Now we need to pass a date as an argument using a function, is there any way to write something like py code.py -p1 a -p2 b -p3 datetime.date.now().

My boss is awsking if we can do this but i have searched and found nothing. Thanks in advance for any answer.

CodePudding user response:

No you cannot. Your shell is not executing python commands.

You can however use your shell capabilities to generate the date, for example with /:

py code.py -p1 a -p2 b -p3 $(date ' %Y-%m-%d')

Would launch the command:

py code.py -p1 a -p2 b -p3 2023-01-12

Another possibility is to pass now as parameter and to handle it in your script.

CodePudding user response:

You can, but beware that it's a big security risk. It will run any code entered at the command line.

text = eval(parameter)
  • Related