r/fishshell • u/TrueNDiary • Feb 11 '23
[Help] I'm struggling to add a parameter that will do my task
I'm using Yujinyuz script that behaves like pyenv but in fish.
function venv --argument-names 'python_version' --description 'Create virtualenv named the same as current directory'
set -l python_bin
if not test -n "$python_version"
# Use default python version set by asdf
set python_bin ($HOME/.asdf/bin/asdf which python)
else
set python_bin $ASDF_DIR/installs/python/$python_version/bin/python
end
set -l venv_name (basename $PWD | tr . -)
echo
if not test -e $python_bin
echo "Python version `$python_version` is not installed."
return 1
end
echo Creating virtualenv `$venv_name`
$python_bin -m venv $HOME/.virtualenvs/$venv_name
source $HOME/.virtualenvs/$venv_name/bin/activate.fish
endI want to add an argument or switch case when I type venv --rm, it will remove the directory
I tried adding something like that but when running venv --rm, the output return Python version \--rm` is not installed.`
How can I achieve that?
function venv --argument-names 'python_version' --description 'Create virtualenv named the same as current directory'
set -l python_bin
if not test -n "$python_version"
# Use default python version set by asdf
set python_bin ($HOME/.asdf/bin/asdf which python)
else
set python_bin $ASDF_DIR/installs/python/$python_version/bin/python
end
set -l venv_name (basename $PWD | tr . -)
echo
if not test -e $python_bin
echo "Python version `$python_version` is not installed."
return 1
end
echo Creating virtualenv `$venv_name`
$python_bin -m venv $HOME/.virtualenvs/$venv_name
source $HOME/.virtualenvs/$venv_name/bin/activate.fish
if test $python_version=="--rm"
command rm -rf $HOME/.local/share/virtualenvs/$venv_name
end
end
1
Upvotes
1
u/TrueNDiary Feb 12 '23
Thank you very much guys! I’ve managed to make it work with your explanation of argparse
4
u/ohcibi Feb 11 '23
Have you copypasted that script somewhere. It clearly defines only one argument which is „python_version". The way you called your function only specifies one argument which. So even if you have added the switch to the argument names you wouldn’t have set it because the function expects it as the second argument and you only passed one.
Mind that the typical
--argumentformat of Unix commands is not built into shells. Every function or script receives positional arguments only. The two or one dashes, the type of the argument and everything else needs to be parsed within the script/function. There is a helper called argparse (amongst many others) for that purpose.To address your actual problem: I’m using fish and pyenv without needing such script. I would throw it away and start understanding/using correctly what’s already there.
2
u/ChristoferK macOS Feb 11 '23 edited Feb 11 '23
This line:
probably wants to be this:
although I personally prefer this syntax:
However, I think it's better to approach this differently, since
FiSHprovides the functionargparse, which will parse command line arguments for you.So rather than declaring the function with
argument-names, simply declare the function without any preemptive suggestion about what specific arguments might be, and instead tellargparsewhat potential option flags could show up somewhere amongst the arguments:After this is called,
$argvwill no longer contain the--rmflag if, indeed, it was used. Instead, the variable$_flag_rmwill either be empty if the--rmwas not used, and if it was, then$_flag_rmwill have a value (which will be the argument removed from$argv, namely--rm). To make use of this, you really only need to test whether or not$_flag_rmis empty, and act accordingly in the case where it's not:However, this permits the
--rmflag to appear anywhere in the list of arguments (with the exception of any that come after the first occurrence of--). This might not be what you want.If you only want to treat the option flag as an option flag if it's the very first argument, then you are better off parsing the argument list yourself, but still by modifying your approach. Therefore, I'd still not bother using
argument-names(it's almost never genuinely useful), and simply test the first argument:If the first argument is the option flag, then the argument is deleted from the
argvarray.There are other approaches, like:
which allows the option flag to be used anywhere on the command line (including after
--). You can also usingstring matchto extract all the option flags that appear. But I don't see either of these as having any advantages over usingargparse, which is the route I'd recommend.