Skip to content

Commit

Permalink
Merge pull request #97 from DannyBen/add/required-catch-all
Browse files Browse the repository at this point in the history
Add support for required `catch_all` arguments
  • Loading branch information
DannyBen committed Aug 27, 2021
2 parents 7ba21b6 + 2771da6 commit a254e6e
Show file tree
Hide file tree
Showing 34 changed files with 251 additions and 90 deletions.
68 changes: 55 additions & 13 deletions examples/catch-all-advanced/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,14 @@ commands:
- name: upload
short: u
help: Upload a file
args:
- name: source
required: true
help: File to upload

flags:
- long: --user
short: -u
arg: user
help: Username to use for logging in
# Configure catch_all for the `upload` sub-command using the extended
# syntax, and specifying that `catch_all` is required (which means that at
# least one extra argument must be provided)
catch_all:
label: Files
help: Files to upload
required: true
- long: --password
short: -p
arg: password
help: Password to use for logging in
```

## Generated script output
Expand Down Expand Up @@ -158,5 +151,54 @@ other_args:

```

### `$ ./cli upload -h`

```shell
cli upload - Upload a file

Shortcut: u

Usage:
cli upload FILES...
cli upload --help | -h

Options:
--help, -h
Show this help

Arguments:
FILES...
Files to upload



```

### `$ ./cli upload`

```shell
missing required argument: FILES...
usage: cli upload FILES...


```

### `$ ./cli upload file1 "file 2" file3`

```shell
# this file is located in 'src/upload_command.sh'
# code for 'cli upload' goes here
# you can edit it freely and regenerate (it will not be overwritten)
args: none

other_args:
- ${other_args[*]} = file1 file 2 file3
- ${other_args[0]} = file1
- ${other_args[1]} = file 2
- ${other_args[2]} = file3


```



72 changes: 14 additions & 58 deletions examples/catch-all-advanced/cli
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ cli_upload_usage() {
echo

printf "Usage:\n"
printf " cli upload SOURCE [options]\n"
printf " cli upload FILES...\n"
printf " cli upload --help | -h\n"
echo

Expand All @@ -122,22 +122,12 @@ cli_upload_usage() {
echo " --help, -h"
printf " Show this help\n"
echo
# :command.usage_flags
# :flag.usage
echo " --user, -u USER (required)"
printf " Username to use for logging in\n"
echo

# :flag.usage
echo " --password, -p PASSWORD"
printf " Password to use for logging in\n"
echo

# :command.usage_args
printf "Arguments:\n"

# :argument.usage
echo " SOURCE"
printf " File to upload\n"
echo " FILES..."
printf " Files to upload\n"
echo

fi
Expand Down Expand Up @@ -248,6 +238,7 @@ parse_requirements() {

esac
done
# :command.catch_all_filter
# :command.default_assignments
# :command.whitelist_filter
}
Expand Down Expand Up @@ -313,6 +304,7 @@ cli_download_parse_requirements() {

esac
done
# :command.catch_all_filter
# :command.default_assignments
# :command.whitelist_filter
}
Expand All @@ -338,66 +330,30 @@ cli_upload_parse_requirements() {
# :command.command_filter
action="upload"
# :command.required_args_filter
if [[ $1 && $1 != -* ]]; then
args[source]=$1
shift
else
printf "missing required argument: SOURCE\nusage: cli upload SOURCE [options]\n"
exit 1
fi
# :command.required_flags_filter
argstring="$*"
if [[ "$argstring" != *--user* && "$argstring" != *-u* ]]; then
printf "missing required flag: --user, -u USER\n"
exit 1
fi
# :command.parse_requirements_while
while [[ $# -gt 0 ]]; do
key="$1"
case "$key" in
# :flag.case
--user | -u )
if [[ $2 ]]; then
args[--user]="$2"
shift
shift
else
printf "%s\n" "--user requires an argument: --user, -u USER"
exit 1
fi
;;

# :flag.case
--password | -p )
if [[ $2 ]]; then
args[--password]="$2"
shift
shift
else
printf "%s\n" "--password requires an argument: --password, -p PASSWORD"
exit 1
fi
;;


-* )
printf "invalid option: %s\n" "$key"
exit 1
other_args+=("$1")
shift
;;

* )
# :command.parse_requirements_case
if [[ ! ${args[source]} ]]; then
args[source]=$1
other_args+=("$1")
shift
else
printf "invalid argument: %s\n" "$key"
exit 1
fi
;;

esac
done
# :command.catch_all_filter
if [[ ${#other_args[@]} -eq 0 ]]; then
printf "missing required argument: FILES...\nusage: cli upload FILES...\n"
exit 1
fi
# :command.default_assignments
# :command.whitelist_filter
}
Expand Down
19 changes: 6 additions & 13 deletions examples/catch-all-advanced/src/bashly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,11 @@ commands:
- name: upload
short: u
help: Upload a file
args:
- name: source
required: true
help: File to upload

flags:
- long: --user
short: -u
arg: user
help: Username to use for logging in
# Configure catch_all for the `upload` sub-command using the extended
# syntax, and specifying that `catch_all` is required (which means that at
# least one extra argument must be provided)
catch_all:
label: Files
help: Files to upload
required: true
- long: --password
short: -p
arg: password
help: Password to use for logging in
6 changes: 5 additions & 1 deletion examples/catch-all-advanced/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ bashly generate
./cli download -h
./cli download source
./cli download source target
./cli download source target and --additional stuff
./cli download source target and --additional stuff

./cli upload -h
./cli upload
./cli upload file1 "file 2" file3
1 change: 1 addition & 0 deletions examples/catch-all/download
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ parse_requirements() {

esac
done
# :command.catch_all_filter
# :command.default_assignments
# :command.whitelist_filter
}
Expand Down
1 change: 1 addition & 0 deletions examples/colors/colorly
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ parse_requirements() {

esac
done
# :command.catch_all_filter
# :command.default_assignments
# :command.whitelist_filter
}
Expand Down
3 changes: 3 additions & 0 deletions examples/command-default/ftp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ parse_requirements() {

esac
done
# :command.catch_all_filter
# :command.default_assignments
# :command.whitelist_filter
}
Expand Down Expand Up @@ -280,6 +281,7 @@ ftp_upload_parse_requirements() {

esac
done
# :command.catch_all_filter
# :command.default_assignments
# :command.whitelist_filter
}
Expand Down Expand Up @@ -336,6 +338,7 @@ ftp_download_parse_requirements() {

esac
done
# :command.catch_all_filter
# :command.default_assignments
# :command.whitelist_filter
}
Expand Down
5 changes: 5 additions & 0 deletions examples/command-groups/ftp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ parse_requirements() {

esac
done
# :command.catch_all_filter
# :command.default_assignments
# :command.whitelist_filter
}
Expand Down Expand Up @@ -353,6 +354,7 @@ ftp_download_parse_requirements() {

esac
done
# :command.catch_all_filter
# :command.default_assignments
# :command.whitelist_filter
}
Expand Down Expand Up @@ -409,6 +411,7 @@ ftp_upload_parse_requirements() {

esac
done
# :command.catch_all_filter
# :command.default_assignments
# :command.whitelist_filter
}
Expand Down Expand Up @@ -453,6 +456,7 @@ ftp_login_parse_requirements() {

esac
done
# :command.catch_all_filter
# :command.default_assignments
# :command.whitelist_filter
}
Expand Down Expand Up @@ -497,6 +501,7 @@ ftp_logout_parse_requirements() {

esac
done
# :command.catch_all_filter
# :command.default_assignments
# :command.whitelist_filter
}
Expand Down
7 changes: 7 additions & 0 deletions examples/commands-nested/cli
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ parse_requirements() {

esac
done
# :command.catch_all_filter
# :command.default_assignments
# :command.whitelist_filter
}
Expand Down Expand Up @@ -437,6 +438,7 @@ cli_dir_parse_requirements() {

esac
done
# :command.catch_all_filter
# :command.default_assignments
# :command.whitelist_filter
}
Expand Down Expand Up @@ -493,6 +495,7 @@ cli_dir_list_parse_requirements() {

esac
done
# :command.catch_all_filter
# :command.default_assignments
# :command.whitelist_filter
}
Expand Down Expand Up @@ -555,6 +558,7 @@ cli_dir_remove_parse_requirements() {

esac
done
# :command.catch_all_filter
# :command.default_assignments
# :command.whitelist_filter
}
Expand Down Expand Up @@ -625,6 +629,7 @@ cli_file_parse_requirements() {

esac
done
# :command.catch_all_filter
# :command.default_assignments
# :command.whitelist_filter
}
Expand Down Expand Up @@ -681,6 +686,7 @@ cli_file_show_parse_requirements() {

esac
done
# :command.catch_all_filter
# :command.default_assignments
# :command.whitelist_filter
}
Expand Down Expand Up @@ -737,6 +743,7 @@ cli_file_edit_parse_requirements() {

esac
done
# :command.catch_all_filter
# :command.default_assignments
# :command.whitelist_filter
}
Expand Down
Loading

0 comments on commit a254e6e

Please sign in to comment.