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

Do not accept options after positional when ARG_LEFTOVERS is specified #114

Open
felipecrs opened this issue Nov 20, 2019 · 3 comments
Open

Comments

@felipecrs
Copy link
Contributor

Simple example, how can I do something like docker run does? You have to specify all the options after run, and then the positional argument which is the image. After, it passes all the remaining arguments (options or not) to the container's entrypoint.

The only way I could accomplish this is by using ARG_POSITIONAL_DOUBLEDASH in my templates, then ensuring the customers don't forget to put --. Example:
Imagine I have a entrypoint.sh in my docker image made with argbash. And also, I have another script called run.sh (also argbash powered), which finds a script in the container and run it. So both entrypoint.sh and run.sh needs ARG_LEFTOVERS.
docker run -ti felipe run.sh another_script.sh -v
It was supposed to pass -v as argument of another_script, but what happens is that the -v is consumed by the entrypoint.sh, which prints its version instead of running run.sh. The way to solve is:
docker run -ti felipe -- run.sh -- another_script.sh -v
But this doesn't seems elegant. I think the best approach would be to treat all the arguments after the positionals as leftovers, just if ARG_LEFTOVERS is specified in template.

@matejak
Copy link
Owner

matejak commented Nov 21, 2019

Hello, I think that I understand your use case and your request.
The problem is that Argbash supports CLI standards - POSIX and GNU. The GNU standard leads to your problem - you are allowed to mix positional and optional arguments, so the only way how to treat part of command-line separately is to use the terminator.
The POSIX mode is supposed to stop looking for optional arguments after the first positional argument is encountered, but it has another issue - as POSIX shells don't have arrays, the leftover args concept is not compatible with them.
In other words, if you really need this non-standard command-line parsing, then Argbash in its current form is not good enough for you. The solution you suggest looks good, but it is not trivial, as it would require another approach in argument parsing code, and, most importantly, the behavior would have to be defined, so it is consistent and in contradiction with existing standards is minimized.

@felipecrs
Copy link
Contributor Author

felipecrs commented Nov 21, 2019

Thanks for the answer.

I also tried the --type posix-script, but then the leftovers array problem you describe arises.

I think this is a real use case that can make Argbash more complete. For example, sudo also works like this. If you run sudo echo -V" it will print in console -V (argument of echo) instead of printing sudos version (-V option for sudo).

I see this behavior used so widely that is weird to see it does not match the standards (or a known exception of it). Despite of it, I completely agree with you on that: if it doesn't match the standards, this behavior must be defined in the template.

@felipecrs
Copy link
Contributor Author

felipecrs commented Nov 21, 2019

An workaround I'm using, is to manually editing the generated .sh code, and adding this line (I have ARG_POSITIONAL_DOUBLEDASH() in my template):

parse_commandline()
{
[...]
			*)
				_last_positional="$1"
				_positionals+=("$_last_positional")
				_positionals_count=$((_positionals_count + 1))
				set -- "${@:1:1}" "--" "${@:2}"
				;;
		esac
		shift
    done
}

The difference is the set -- "${@:1:1}" "--" "${@:2}" below _positionals_count=$((_positionals_count + 1)).

What it does is that it automatically puts -- when the first positional argument is found.

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

No branches or pull requests

2 participants