diff --git a/README.md b/README.md index d48a59a..aa75f35 100644 --- a/README.md +++ b/README.md @@ -197,6 +197,20 @@ _is_used_command bar && echo 'Command "bar" 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 Outputs the number of positional arguments. @@ -239,6 +253,20 @@ Option "--bar" 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 Outputs the number of arguments for the option with the alias passed as the first argument. diff --git a/parser.sh b/parser.sh index 9cb45fd..e0cb4a2 100755 --- a/parser.sh +++ b/parser.sh @@ -1007,6 +1007,24 @@ _is_used_command() { [ -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() { echo "$_positional_args_count" } @@ -1030,6 +1048,25 @@ _is_used_option() { [ -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() { _option_index=$(_get_mapped_option_index_by_alias "$1")