update command line argument parser (#19)
* update command line argument parser * update with PR feedback
This commit is contained in:
Родитель
d7cd9f85e3
Коммит
f56d95b67c
|
@ -88,18 +88,10 @@ log_init
|
|||
VERSION_TAG="v0.0.0-rc0"
|
||||
|
||||
# add flag:variable_name dictionary entries
|
||||
add_option_args -v "VERBOSE_LOGGING"
|
||||
add_option_args --verbose "VERBOSE_LOGGING"
|
||||
add_option_args -dp "DEVICE_PROVISIONING"
|
||||
add_option_args --device-provisioning "DEVICE_PROVISIONING"
|
||||
add_option_args -ap "AZURE_CLOUD_IDENTITY_PROVIDER"
|
||||
add_option_args --azure-cloud-identity-provider "AZURE_CLOUD_IDENTITY_PROVIDER"
|
||||
add_option_args -s "SCOPE_ID"
|
||||
add_option_args --scope-id "SCOPE_ID"
|
||||
add_option_args -r "REGISTRATION_ID"
|
||||
add_option_args --registration-id "REGISTRATION_ID"
|
||||
add_option_args -k "SYMMETRIC_KEY"
|
||||
add_option_args --symmetric-key "SYMMETRIC_KEY"
|
||||
add_option_args "VERBOSE_LOGGING" -v --verbose
|
||||
add_option_args "SCOPE_ID" -s --scope-id
|
||||
add_option_args "REGISTRATION_ID" -r --registration-id
|
||||
add_option_args "SYMMETRIC_KEY" -k --symmetric-key
|
||||
|
||||
# parse command line inputs and fetch output from parser
|
||||
declare -A parsed_cmds="$(cmd_parser $@)"
|
||||
|
@ -116,6 +108,8 @@ fi
|
|||
if [[ "${parsed_cmds["SCOPE_ID"]}" == "" || "${parsed_cmds["REGISTRATION_ID"]}" == "" || "${parsed_cmds["SYMMETRIC_KEY"]}" == "" ]];
|
||||
then
|
||||
echo Missing argument
|
||||
echo defined: "'"${!parsed_cmds[@]}"'"
|
||||
echo given: "'"${parsed_cmds[@]}"'"
|
||||
echo Usage
|
||||
exit 1
|
||||
fi
|
||||
|
|
93
src/utils.sh
93
src/utils.sh
|
@ -3,56 +3,101 @@
|
|||
# Copyright (c) Microsoft Corporation.
|
||||
# Licensed under the MIT License.
|
||||
|
||||
VERSION_TAG="v0.0.0-rc0"
|
||||
VERSION_TAG="v0.0.1"
|
||||
|
||||
# create flag:variable_name dictionary
|
||||
declare -A flag_to_variable_dict
|
||||
|
||||
add_option_args() {
|
||||
######################################
|
||||
# add_option_args
|
||||
#
|
||||
# declare a valid command-line argument(s)
|
||||
#
|
||||
# ARGUMENTS:
|
||||
# OPTION_NAME The name of the argument (e.g "VERBOSE")
|
||||
# a list of one or more argument switches (e.g "-v", "--verbose")
|
||||
# OUTPUTS:
|
||||
# Write output to stdout
|
||||
# RETURN:
|
||||
#
|
||||
######################################
|
||||
|
||||
function add_option_args() {
|
||||
if [[ $# > 1 ]];
|
||||
then
|
||||
local option_flag=$1; shift
|
||||
local var_name=$1; shift
|
||||
flag_to_variable_dict[$option_flag]=$var_name
|
||||
local option_name=$1; shift
|
||||
while [ $# -ne 0 ];
|
||||
do
|
||||
flag_to_variable_dict[$1]=$option_name
|
||||
shift
|
||||
done
|
||||
fi
|
||||
}
|
||||
|
||||
# generic command line parser
|
||||
function cmd_parser() {
|
||||
# create flag:variable_name dictionary
|
||||
declare -A parsed_cmd
|
||||
######################################
|
||||
# clear_option_args
|
||||
#
|
||||
# clears the list of valid command-line arguments
|
||||
#
|
||||
# ARGUMENTS:
|
||||
#
|
||||
# OUTPUTS:
|
||||
#
|
||||
# RETURN:
|
||||
#
|
||||
######################################
|
||||
|
||||
for arg in "$@"
|
||||
function clear_option_args() {
|
||||
flag_to_variable_dict=()
|
||||
}
|
||||
|
||||
function cmd_parser() {
|
||||
# create flag:variable_name dictionary and initialize to empty string
|
||||
declare -A parsed_cmd
|
||||
for key in ${!flag_to_variable_dict[*]};
|
||||
do
|
||||
if [[ $arg == -* ]];
|
||||
parsed_cmd[${flag_to_variable_dict[$key]}]=""
|
||||
done
|
||||
|
||||
while [ $# -ne 0 ];
|
||||
do
|
||||
if [[ $1 == -* ]];
|
||||
then
|
||||
# iterate over all the keys in dictionary
|
||||
local valid_arg="false"
|
||||
for k in ${!flag_to_variable_dict[*]};
|
||||
local valid_argument=false
|
||||
# for each key in the dictionary
|
||||
for key in ${!flag_to_variable_dict[*]};
|
||||
do
|
||||
# if arg==key, then we store into flag_to_val_dict
|
||||
if [ "$arg" == "$k" ];
|
||||
if [ "$1" == "$key" ]
|
||||
then
|
||||
# set parsed_cmd, which is varname:value
|
||||
parsed_cmd[${flag_to_variable_dict[$k]}]=$2
|
||||
valid_arg="true"
|
||||
valid_argument=true
|
||||
if [[ $# == 1 || $2 == -* ]];
|
||||
then
|
||||
parsed_cmd[${flag_to_variable_dict[$key]}]=true
|
||||
else
|
||||
parsed_cmd[${flag_to_variable_dict[$key]}]=$2
|
||||
shift
|
||||
fi
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
# found an unknown argument
|
||||
if [[ "$valid_arg" == "false" ]];
|
||||
if [[ "$valid_argument" == "false" ]];
|
||||
then
|
||||
parsed_cmd=()
|
||||
break
|
||||
fi
|
||||
else
|
||||
parsed_cmd=()
|
||||
break
|
||||
fi
|
||||
shift # Remove argument name from processing
|
||||
|
||||
shift
|
||||
done
|
||||
|
||||
# view content of entire dictionary
|
||||
echo '('
|
||||
for key in "${!parsed_cmd[@]}";
|
||||
for key in "${!parsed_cmd[@]}";
|
||||
do
|
||||
echo "[$key]=${parsed_cmd[$key]}"
|
||||
done
|
||||
|
@ -121,9 +166,7 @@ export -f log_init log_error log_info log_warn log_debug
|
|||
######################################
|
||||
# prepare_apt
|
||||
#
|
||||
# installs Azure IoT Edge Runtime 1.2
|
||||
# generates the edge's configuration file from template and
|
||||
# fills in the DPS provisioning section from provided parameters
|
||||
# adds the needed microsoft sources list and key to the apt repository
|
||||
#
|
||||
# ARGUMENTS:
|
||||
# OS_PLATFORM - a string specifying the location of specific platform files
|
||||
|
|
|
@ -3,13 +3,22 @@
|
|||
# import utils.sh
|
||||
source ../../src/utils.sh
|
||||
|
||||
# add flag:variable_name dictionary entries
|
||||
add_option_args "VERBOSE_LOGGING" -v --verbose
|
||||
add_option_args "SCOPE_ID" -s --scope-id
|
||||
add_option_args "REGISTRATION_ID" -r --registration-id
|
||||
add_option_args "SYMMETRIC_KEY" -k --symmetric-key
|
||||
add_option_args "DEVICE_PROVISIONING" -dp
|
||||
add_option_args "AZURE_CLOUD_ID" -ap
|
||||
|
||||
# unit test to test all illegal flags, output should be empty
|
||||
test_illegal_flags() {
|
||||
# parse sample input to parser
|
||||
declare -A parsed_cmds="$(cmd_parser -illegal val -t anotherillegalval -z yetanotherillegalval)"
|
||||
|
||||
# compare output, should be empty
|
||||
if [ "${parsed_cmds[*]}" != "" ]; then
|
||||
if [ "${parsed_cmds[*]}" != "" ];
|
||||
then
|
||||
echo "Failed to pass unit test 'test_illegal_flags' in test-cmd-parser.sh. Non-empty result: ${parsed_cmds[*]}"
|
||||
fi
|
||||
}
|
||||
|
@ -19,7 +28,8 @@ test_all_legal_flags() {
|
|||
declare -A parsed_cmds="$(cmd_parser -dp 1 -ap 5 -s 2 -r 4 -k 3 -v 6)"
|
||||
|
||||
# compare output, should be 123456
|
||||
if [ "${parsed_cmds[*]}" != "1 2 3 4 5 6" ]; then
|
||||
if [[ "${parsed_cmds['DEVICE_PROVISIONING']}" != "1" || "${parsed_cmds['AZURE_CLOUD_ID']}" != "5" || "${parsed_cmds['SCOPE_ID']}" != "2" || "${parsed_cmds['REGISTRATION_ID']}" != "4" || "${parsed_cmds['SYMMETRIC_KEY']}" != "3" || "${parsed_cmds['VERBOSE_LOGGING']}" != "6" ]];
|
||||
then
|
||||
echo "Failed to pass unit test 'test_all_legal_flags' in test-cmd-parser.sh. Non-empty result: ${parsed_cmds[*]}"
|
||||
fi
|
||||
}
|
||||
|
@ -29,8 +39,9 @@ test_extra_arguments() {
|
|||
declare -A parsed_cmds="$(cmd_parser illegalinput 0 -ap 4 -s 1 -r 3 more -extra -k 2)"
|
||||
|
||||
# compare output, should be 1 2 3 4
|
||||
if [ "${parsed_cmds[*]}" != "1 2 3 4" ]; then
|
||||
echo "Failed to pass unit test 'test_extra_arguments' in test-cmd-parser.sh. Non-empty result: ${parsed_cmds[*]}"
|
||||
if [ "${parsed_cmds[*]}" != "" ];
|
||||
then
|
||||
echo "Failed to pass unit test 'test_extra_arguments' in test-cmd-parser.sh"
|
||||
fi
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче