Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage message should reflect when a set of accepted values is restricted. #76

Closed
hemna opened this issue Apr 10, 2019 · 5 comments · Fixed by #88
Closed

Usage message should reflect when a set of accepted values is restricted. #76

hemna opened this issue Apr 10, 2019 · 5 comments · Fixed by #88

Comments

@hemna
Copy link

hemna commented Apr 10, 2019

Is there a way to provide a list of valid positional values?

I want my script to accept a single positional argument where the command has a specific list of valid commands that it accepts? Say the list is: start, stop, deploy, remove
if a user tries to enter a command such as 'foo' then the help comes up and says the command is not a valid command, here is the list and each command's description and help.

./my_script.sh

./my_script.sh deploy
./my_script.sh stop
./my_script.sh start

./my_script.sh foo
this one fails and says that foo isn't a valid command, and provides a list of valid commands to pass and their help (description).

@matejak
Copy link
Owner

matejak commented Apr 10, 2019

Take a look at argbash guide, and check out the ARG_TYPE_GROUP_SET macro.

@hemna
Copy link
Author

hemna commented Apr 10, 2019

ok so I'm trying to do this.

#!/bin/bash

validcommands="deploy_ses,deploy_caasp,deploy_ccp_deployer"
Field_Separator=$IFS
cmds=""
IFS=,
for cmd in $validcommands; do
  cmds+="$cmd ";
done
IFS=$Field_Separator
commandshelp="The allowed commands are $cmds"


# m4_ignore(
echo "This is just a parsing library template, not the library - pass the parent script 'ass.m4' to 'argbash' to fix this." >&2
exit 11  #)Created by argbash-init v2.8.0
# ARG_POSITIONAL_SINGLE([command], [$commandshelp])
# ARG_TYPE_GROUP_SET([commands], [COMMAND], [command], [$validcommands], [index])
# ARG_DEFAULTS_POS
# ARG_HELP([<The general help message of my script>])
# ARGBASH_GO
┌─[waboring@walt-dev2] - [~/tmp] - [Wed Apr 10, 23:09]
└─[$] <> argbash -o test ass.m4
The --library option is deprecated, use --strip user-content next time
┌─[waboring@walt-dev2] - [~/tmp] - [Wed Apr 10, 23:09]
└─[$] <> ./test -h
<The general help message of my script>
Usage: ./test [-h|--help] <command>
	<command>: The allowed commands are deploy_ses deploy_caasp deploy_ccp_deployer
	-h, --help: Prints help
┌─[waboring@walt-dev2] - [~/tmp] - [Wed Apr 10, 23:09]
└─[$] <> ./test deploy_ses
Value 'deploy_ses' (of argument 'command') doesn't match the list of allowed values: 'deploy_ses,deploy_caasp,deploy_ccp_deployer'

@hemna
Copy link
Author

hemna commented Apr 10, 2019

since argbash doesn't show the list of valid options in help, I have to try a workaround.
Unforunately it doesn't work either. It shows the help that I'd like, but since the option is a var $validcommands, argbash doesn't seem to be able to recognize the input on the cmdln is valid.

@hemna
Copy link
Author

hemna commented Apr 10, 2019

If I change the declaration to

# m4_ignore(
echo "This is just a parsing library template, not the library - pass the parent script 'ass.m4' to 'argbash' to fix this." >&2
exit 11  #)Created by argbash-init v2.8.0
# ARG_POSITIONAL_SINGLE([command], [$commandshelp])
# ARG_TYPE_GROUP_SET([commands], [COMMAND], [command], [deploy_ses,deploy_ass], [index])
# ARG_DEFAULTS_POS
# ARG_HELP([<The general help message of my script>])
# ARGBASH_GO

It works, but then I have to redeclare the same options multiple times (one for building the help text, and again to show the valid options).

┌─[waboring@walt-dev2] - [~/tmp] - [Wed Apr 10, 23:15]
└─[$] <> ./test test
Value 'test' (of argument 'command') doesn't match the list of allowed values: 'deploy_ses' and 'deploy_ass'

@matejak
Copy link
Owner

matejak commented Apr 11, 2019

Yes, you are doing it right. The code generator can't have access to bash variables, so you have to repeat it for the help message text.
It is true that the help message should indicate the set of possible values.

@matejak matejak changed the title valid argument values? Usage message should reflect when a set of accepted values is restricted. Apr 11, 2019
matejak added a commit that referenced this issue May 12, 2019
When there are args of the on-of type,
the help message and docopt output
inform the user accordingly.

Fixes: #76
matejak added a commit that referenced this issue May 12, 2019
When there are args of the on-of type,
the help message and docopt output
inform the user accordingly.

Fixes: #76
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants