13

The alias for Bash fails with the default arguments for the python and argparse...

 3 years ago
source link: https://www.codesd.com/item/the-alias-for-bash-fails-with-the-default-arguments-for-the-python-and-argparse-script.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

The alias for Bash fails with the default arguments for the python and argparse script?

advertisements

This question already has an answer here:

  • Make a Bash alias that takes a parameter? 10 answers

I have the following issue:

I have multiple bash aliases for a python script: The python script is using arg-parse with two arugemnts, one is optional.

testing.py -i value1
testing.py -i value1 -e value2

alias test1=' ./testing.py -i $1'

This works as expected

alias test2=' ./testing.py -i $1 -e $2'

This doesn't work!

ultimately, would like to do this from the command line:

test2 value1 value2

I've searched bash functions, not entirely sure if it will solve my problem. I've tried numerous ways to escape, qoute, hack around it, rearrange.. etc.. no dice

It would be very helpful to be able to pass the second default argument in bash $2 to the -e argparse option for the python script.

Any help would be greatly appreciated..

Sincerely,

Thanks!


Aliases are simple prefix expansion. They don't know what their arguments are and can't run conditional logic.

test1() { ./testing.py -i "$1"; }

test2() { ./testing.py -i "$1" -e "$2"; }

...or, better, a single function that handles both cases:

testfunc() { ./testing.py ${1+ -i "$1"} ${2+ -e "$2"}; }


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK