简体   繁体   中英

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

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.

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.

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

text = eval(parameter)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM