Add arg listing functions

This commit is contained in:
2024-09-11 12:05:12 +00:00
parent 45138ce80c
commit eb4927a3ee
2 changed files with 65 additions and 0 deletions

View File

@@ -197,6 +197,20 @@ _is_used_command bar && echo 'Command "bar" was used'
Command "foo" was used Command "foo" was used
``` ```
#### _get_positional_args
Outputs the positional arguments separated by space. Delimiter can be changed by passing it's value as the first argument.
```bash
_parse foo bar baz
_get_positional_args
_get_positional_args "|"
```
```
foo bar baz
foo|bar|baz
```
#### _get_positional_args_count #### _get_positional_args_count
Outputs the number of positional arguments. Outputs the number of positional arguments.
@@ -239,6 +253,20 @@ Option "--bar" was used
Option "-c" was used Option "-c" was used
``` ```
#### _get_option_args
Outputs the arguments of the option separated by space. Mandatory option alias is expected as the first argument. Delimiter can be changed by passing it's value as the second argument.
```bash
_parse -v foo bar baz
_get_option_args -v
_get_option_args -v "|"
```
```
foo bar baz
foo|bar|baz
```
#### _get_option_args_count #### _get_option_args_count
Outputs the number of arguments for the option with the alias passed as the first argument. Outputs the number of arguments for the option with the alias passed as the first argument.

View File

@@ -1007,6 +1007,24 @@ _is_used_command() {
[ -n "$_command_index" ] && [ -n "$_used_command_index" ] && [ "$_command_index" -eq "$_used_command_index" ] [ -n "$_command_index" ] && [ -n "$_used_command_index" ] && [ "$_command_index" -eq "$_used_command_index" ]
} }
_get_positional_args() {
_delimiter=${1:-" "}
if [ "$_positional_args_count" -gt "1" ]; then
_positional_args=""
_i=1
while [ "$_i" -le "$_positional_args_count" ]; do
_positional_arg=$(_var_value "_positional_args_${_i}")
_positional_args="${_positional_args}${_delimiter}${_positional_arg}"
_i=$(_math "$_i + 1")
done
echo "${_positional_args#"$_delimiter"}"
fi
}
_get_positional_args_count() { _get_positional_args_count() {
echo "$_positional_args_count" echo "$_positional_args_count"
} }
@@ -1030,6 +1048,25 @@ _is_used_option() {
[ -n "$_option_index" ] && _is "$(_var_value "_options_${_option_index}_used")" [ -n "$_option_index" ] && _is "$(_var_value "_options_${_option_index}_used")"
} }
_get_option_args() {
_option_index=$(_get_mapped_option_index_by_alias "$1")
_delimiter=${2:-" "}
_option_args=""
if [ -n "$_option_index" ]; then
_option_args_count=$(_var_value "_options_${_option_index}_args_count")
_i=1
while [ "$_i" -le "$_option_args_count" ]; do
_arg=$(_var_value "_options_${_option_index}_args_${_i}")
_option_args="${_option_args}${_delimiter}${_arg}"
_i=$(_math "$_i + 1")
done
echo "${_option_args#"$_delimiter"}"
fi
}
_get_option_args_count() { _get_option_args_count() {
_option_index=$(_get_mapped_option_index_by_alias "$1") _option_index=$(_get_mapped_option_index_by_alias "$1")