From 7293217f3d7188d156fe49101f3d691f9500b392 Mon Sep 17 00:00:00 2001 From: danyboy1104 Date: Mon, 24 Mar 2014 15:38:25 -0400 Subject: [PATCH 1/9] The changes were commited --- README.md | 38 ++++++++++++++++++----- optparse.bash | 47 +++++++++++++++++++++++++---- sample_event.sh | 14 +++++++++ sample_event_generate_completion.sh | 18 +++++++++++ 4 files changed, 104 insertions(+), 13 deletions(-) create mode 100755 sample_event.sh create mode 100755 sample_event_generate_completion.sh diff --git a/README.md b/README.md index 09f6978..ea79b02 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,26 @@ #Optparse -A BASH wrapper for getopts, for simple command-line argument parsing +A BASH wrapper for getopts and compgen, for simple command-line argument parsing an bash completion. ##What is this? -A wrapper that provides a clean and easy way to parse arguments to your BASH scripts. It lets you define short and long option names, handle flag variables, and set default values for optional arguments, all while aiming to be as minimal as possible: *One line per argument definition*. +A wrapper that provides a clean and easy way to parse arguments and create Tab completion to your BASH scripts. It lets you define short and long option names, handle flag variables, set default values for optional arguments and define an optional list of posible values for options to complete, all while aiming to be as minimal as possible: *One line per argument definition*. ##Usage -##### See `sample_head.sh` for a demonstration of optparse -###1. Define your arguments + +###1. Source file optparse.bash +###2. Define your arguments +#### Generate scripts for completion an option parsing using optparse.bash. +#### Source profile configuration +##### See `sample_event_generate_completion.sh` for a demonstration. + +#### Source the optparse generated file in your command script( The script to parse arguments and generate completion ). +##### See `sample_event.sh` for a demonstration. +### Demo +./sample_event.sh [TAB][TAB] +--name --country --year +./sample_event.sh --country [TAB][TAB] +USA Canada Japan Brasil +./sample_event.sh --name Football World Cup --country Brasil --year 2014 +The Football World Cup will be in Brasil in 2014. Each argument to the script is defined with `optparse.define`, which specifies the option names, a short description, the variable it sets and the default value (if any). @@ -18,13 +32,21 @@ Flags are defined in exactly the same way, but with an extra parameter `value` t ```bash optparse.define short=v long=verbose desc="Set flag for verbose mode" variable=verbose_mode value=true default=false -``` +``` + +```bash +optparse.define short=f long=file desc="The input file" variable=filename list="word1 word2 word3" +``` + +```bash +optparse.define short=f long=file desc="The input file" variable=filename list="\$(my_command)" +``` ###2. Evaluate your arguments -The `optparse.build` function creates a temporary header script based on the provided argument definitions. Simply source the file the function returns, to parse the arguments. +The `optparse.build` function creates a temporary header script and a configuration file in /etc/bash_completion.d/ based on the provided argument definitions. Simply source the file the function returns and also pass a script name as argument, to parse the arguments and generate completion. ```bash -source $( optparse.build ) +source $( optparse.build ) script_name ``` ####That's it! @@ -53,6 +75,8 @@ the value to set the variable to. If unspecified, user is expected to provide a a short description of the argument (to build the usage description) ####`default`(optional) the default value to set the variable to if argument not specified +####`list`(optional) +the list of posible arguments for an option to autocomplete. It could be a list of strings or a command. ##Installation 1. Download/clone `optparse.bash` diff --git a/optparse.bash b/optparse.bash index 731e7f5..f99ace8 100644 --- a/optparse.bash +++ b/optparse.bash @@ -1,4 +1,4 @@ -# Optparse - a BASH wrapper for getopts +# Optparse - a BASH wrapper for getoptions # @author : nk412 / nagarjuna.412@gmail.com optparse_usage="" @@ -6,6 +6,8 @@ optparse_contractions="" optparse_defaults="" optparse_process="" optparse_arguments_string="" +optparse_process_completion="" +options="" # ----------------------------------------------------------------------------------------------------------------------------- function optparse.throw_error(){ @@ -45,6 +47,8 @@ function optparse.define(){ local variable="$value" elif [ "$key" = "value" ]; then local val="$value" + elif [ "$key" = "list" ]; then + local list="$value" fi done @@ -57,7 +61,7 @@ function optparse.define(){ fi # build OPTIONS and help - optparse_usage="${optparse_usage}#NL#TB${short} $(rpad "$long:" 25) ${desc}" + optparse_usage="${optparse_usage}#NL#TB${short} $(printf "%-25s %s" "${long}:" "${desc}")" if [ "$default" != "" ]; then optparse_usage="${optparse_usage} [default:$default]" fi @@ -70,11 +74,21 @@ function optparse.define(){ optparse_arguments_string="${optparse_arguments_string}:" fi optparse_process="${optparse_process}#NL#TB#TB${shortname})#NL#TB#TB#TB${variable}=\"$val\";;" + + # Complete options + options="${options} ${long}" + # Complete command arguments + if [ "$list" != "" ]; then + optparse_process_completion="${optparse_process_completion}#NL#TB#TB--${variable})#NL#TB#TB#TB${variable}_list=\"$list\"#NL#TB#TB#TBCOMPREPLY=( \$(compgen -W \"\${${variable}_list}\" -- \${cur}) )#NL#TB#TB#TBreturn 0;;" + fi + } # ----------------------------------------------------------------------------------------------------------------------------- function optparse.build(){ - local build_file="/tmp/optparse-${RANDOM}.tmp" + local script=${1:?} + local build_file="${script}_optparse" + local completion_file="/etc/bash_completion.d/${script}" # Building getopts header here @@ -132,12 +146,35 @@ while getopts "$optparse_arguments_string" option; do esac done +EOF + +# Create completion script + cat << EOF > $completion_file +_$script(){ + local cur prev options + COMPREPLY=() + cur=\${COMP_WORDS[COMP_CWORD]} + prev=\${COMP_WORDS[COMP_CWORD-1]} + + # The basic options we'll complete. + options="${options}" + + # Complete the arguments to some of the basic commands. + case \$prev in + $optparse_process_completion + *) + esac + COMPREPLY=(\$(compgen -W "\${options}" -- \${cur})) + return 0 +} +complete -F _$script $script EOF local -A o=( ['#NL']='\n' ['#TB']='\t' ) for i in "${!o[@]}"; do sed -i "s/${i}/${o[$i]}/g" $build_file + sed -i "s/${i}/${o[$i]}/g" $completion_file done # Unset global variables @@ -146,8 +183,6 @@ EOF unset optparse_arguments_string unset optparse_defaults unset optparse_contractions - - # Return file name to parent - echo "$build_file" + unset options } # ----------------------------------------------------------------------------------------------------------------------------- diff --git a/sample_event.sh b/sample_event.sh new file mode 100755 index 0000000..bb3f335 --- /dev/null +++ b/sample_event.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +# Source the sample_event_optparse file --------------------------------------------------- +source sample_event.sh_optparse + +if [ -z "$name" ] || [ -z "$country" ] || [ -z "$year" ]; then + usage; exit 1 +fi + +# Display event information +echo "The $name event will be in $country in $year." + +exit 0 + diff --git a/sample_event_generate_completion.sh b/sample_event_generate_completion.sh new file mode 100755 index 0000000..19d9b58 --- /dev/null +++ b/sample_event_generate_completion.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +# Source the optparse.bash file --------------------------------------------------- +source optparse.bash + +# Define options +optparse.define short=n long=name desc="The event name" variable=name list="fgh" +optparse.define short=c long=country desc="The event country" variable=country list="USA Canada Japan Brasil" +optparse.define short=d long=year desc="The event year" variable=year list="2006 2010 2014 2020" + +# Generate optparse and autocompletion scripts +script_name="sample_event.sh" +optparse.build $script_name + +# Source completion configuration +bash + +exit 0 From d348191742f36beac3eaba7fd457e5ebb01ca1fa Mon Sep 17 00:00:00 2001 From: danyboy1104 Date: Wed, 26 Mar 2014 11:24:25 -0400 Subject: [PATCH 2/9] Readme updated. In optparse.bash completion files in $HOME/.bash_completion.d/* --- README.md | 58 +++++++++++++++-------------- optparse.bash | 8 ++-- sample_event_generate_completion.sh | 7 +--- 3 files changed, 38 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index ea79b02..e833073 100644 --- a/README.md +++ b/README.md @@ -2,27 +2,15 @@ A BASH wrapper for getopts and compgen, for simple command-line argument parsing an bash completion. ##What is this? -A wrapper that provides a clean and easy way to parse arguments and create Tab completion to your BASH scripts. It lets you define short and long option names, handle flag variables, set default values for optional arguments and define an optional list of posible values for options to complete, all while aiming to be as minimal as possible: *One line per argument definition*. +A wrapper that provides a clean and easy way to parse arguments and create Tab completion to your BASH scripts. It lets you define short and long option names, handle flag variables, set default values for optional arguments and an optional list of posible values for options to complete, all while aiming to be as minimal as possible: *One line per argument definition*. ##Usage +###1. Install bash-completion package. +###2. Create a script to generate completion and option parsing files. +###3. Source file optparse.bash in the script. +###4. Define your arguments in the script. -###1. Source file optparse.bash -###2. Define your arguments -#### Generate scripts for completion an option parsing using optparse.bash. -#### Source profile configuration -##### See `sample_event_generate_completion.sh` for a demonstration. - -#### Source the optparse generated file in your command script( The script to parse arguments and generate completion ). -##### See `sample_event.sh` for a demonstration. -### Demo -./sample_event.sh [TAB][TAB] ---name --country --year -./sample_event.sh --country [TAB][TAB] -USA Canada Japan Brasil -./sample_event.sh --name Football World Cup --country Brasil --year 2014 -The Football World Cup will be in Brasil in 2014. - -Each argument to the script is defined with `optparse.define`, which specifies the option names, a short description, the variable it sets and the default value (if any). +Each argument to the script is defined with `optparse.define`, which specifies the option names, a short description, the variable it sets, the default value (if any) and a list of posible values (if any). ```bash optparse.define short=f long=file desc="The input file" variable=filename @@ -35,32 +23,48 @@ optparse.define short=v long=verbose desc="Set flag for verbose mode" variable=v ``` ```bash -optparse.define short=f long=file desc="The input file" variable=filename list="word1 word2 word3" +optparse.define short=f long=file desc="The input file" variable=filename list="string1 string2 string3" ``` ```bash optparse.define short=f long=file desc="The input file" variable=filename list="\$(my_command)" -``` -###2. Evaluate your arguments -The `optparse.build` function creates a temporary header script and a configuration file in /etc/bash_completion.d/ based on the provided argument definitions. Simply source the file the function returns and also pass a script name as argument, to parse the arguments and generate completion. +###5. Evaluate your arguments +The `optparse.build` function creates a header script and a configuration file in /etc/bash_completion.d/ based on the provided argument definitions. ```bash -source $( optparse.build ) script_name +optparse.build script_name ``` +###5. Allow execution to the script and execute it. +##### See `sample_event_generate_completion.sh` for a demonstration. +###6. Source profile bash completion configuration, example: +source $HOME/.bash_completion.d/* + +###7. In your command script( The script to parse arguments and generate completion ) source the optparse generated file to parse and evaluate arguments. + ####That's it! -The script can now make use of the variables. Running the script (without any arguments) should give you a neat usage description. +The script can now show completion and make use of the variables. Running the script (without any arguments) should give you a neat usage description. usage: ./script.sh [OPTIONS] OPTIONS: - -f --file : The input file - -v --verbose : Set flag for verbose mode + -n --name : The event name + -c --country : The country name + -y --year : The year -? --help : usage - + +##### See `sample_event.sh` for a demonstration. +### Now to use the command script we can write: +./sample_event.sh [TAB][TAB] +--name --country --year +./sample_event.sh --country [TAB][TAB] +USA Canada Japan Brasil +./sample_event.sh --name "Football World Cup" --country Brasil --year 2014 +The Football World Cup will be in Brasil in 2014. + ##Supported definition parameters All definition parameters for `optparse.define` are provided as `key=value` pairs, seperated by an `=` sign. ####`short` diff --git a/optparse.bash b/optparse.bash index f99ace8..90a0e38 100644 --- a/optparse.bash +++ b/optparse.bash @@ -88,7 +88,8 @@ function optparse.define(){ function optparse.build(){ local script=${1:?} local build_file="${script}_optparse" - local completion_file="/etc/bash_completion.d/${script}" + local completion_dir="$HOME/.bash_completion.d/" + local completion_file="${completion_dir}${script}" # Building getopts header here @@ -149,16 +150,17 @@ done EOF # Create completion script +mkdir -p $completion_dir cat << EOF > $completion_file _$script(){ local cur prev options COMPREPLY=() cur=\${COMP_WORDS[COMP_CWORD]} prev=\${COMP_WORDS[COMP_CWORD-1]} - + # The basic options we'll complete. options="${options}" - + # Complete the arguments to some of the basic commands. case \$prev in $optparse_process_completion diff --git a/sample_event_generate_completion.sh b/sample_event_generate_completion.sh index 19d9b58..bbbc4b3 100755 --- a/sample_event_generate_completion.sh +++ b/sample_event_generate_completion.sh @@ -4,15 +4,12 @@ source optparse.bash # Define options -optparse.define short=n long=name desc="The event name" variable=name list="fgh" -optparse.define short=c long=country desc="The event country" variable=country list="USA Canada Japan Brasil" +optparse.define short=n long=name desc="The event name" variable=name +optparse.define short=c long=country desc="The event country" variable=country list="USA Canada Japan Brasil England" optparse.define short=d long=year desc="The event year" variable=year list="2006 2010 2014 2020" # Generate optparse and autocompletion scripts script_name="sample_event.sh" optparse.build $script_name -# Source completion configuration -bash - exit 0 From 0340ec529e11af290786e61cd6fd420f38bd9a39 Mon Sep 17 00:00:00 2001 From: danyboy1104 Date: Wed, 26 Mar 2014 11:35:05 -0400 Subject: [PATCH 3/9] Readme modified --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e833073..b8d6f5f 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ optparse.define short=f long=file desc="The input file" variable=filename list=" optparse.define short=f long=file desc="The input file" variable=filename list="\$(my_command)" ###5. Evaluate your arguments -The `optparse.build` function creates a header script and a configuration file in /etc/bash_completion.d/ based on the provided argument definitions. +The `optparse.build` function creates a header script and a configuration file in $HOME/.bash_completion.d/ based on the provided argument definitions. ```bash optparse.build script_name From c2b439941fcdacf9f4c3e9238cd100e78d82fe26 Mon Sep 17 00:00:00 2001 From: danyboy1104 Date: Wed, 26 Mar 2014 11:38:11 -0400 Subject: [PATCH 4/9] Readme modified --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b8d6f5f..18c2ea1 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ optparse.define short=f long=file desc="The input file" variable=filename list=" ```bash optparse.define short=f long=file desc="The input file" variable=filename list="\$(my_command)" +``` ###5. Evaluate your arguments The `optparse.build` function creates a header script and a configuration file in $HOME/.bash_completion.d/ based on the provided argument definitions. From 5e50d01f805f5558dfc0f24d8b48cbe06c110f91 Mon Sep 17 00:00:00 2001 From: danyboy1104 Date: Wed, 26 Mar 2014 13:41:56 -0400 Subject: [PATCH 5/9] Readme.md modified --- README.md | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 18c2ea1..8814580 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ optparse.define short=f long=file desc="The input file" variable=filename list=" The `optparse.build` function creates a header script and a configuration file in $HOME/.bash_completion.d/ based on the provided argument definitions. ```bash -optparse.build script_name +$ optparse.build script_name ``` ###5. Allow execution to the script and execute it. @@ -59,12 +59,15 @@ The script can now show completion and make use of the variables. Running the sc ##### See `sample_event.sh` for a demonstration. ### Now to use the command script we can write: -./sample_event.sh [TAB][TAB] ---name --country --year -./sample_event.sh --country [TAB][TAB] -USA Canada Japan Brasil -./sample_event.sh --name "Football World Cup" --country Brasil --year 2014 -The Football World Cup will be in Brasil in 2014. +```bash +$ ./sample_event.sh [TAB][TAB] +$ --name --country --year +$ ./sample_event.sh --country [TAB][TAB] +$ USA Canada Japan Brasil +$ ./sample_event.sh --name "Football World Cup" --country Brasil --year 2014 +$ The Football World Cup will be in Brasil in 2014. +``` + ##Supported definition parameters All definition parameters for `optparse.define` are provided as `key=value` pairs, seperated by an `=` sign. From db2f9a13d6bf1e6f22e7637ef5eaad02554ac282 Mon Sep 17 00:00:00 2001 From: danyboy1104 Date: Wed, 26 Mar 2014 14:22:55 -0400 Subject: [PATCH 6/9] Readme.md modified --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8814580..0e21063 100644 --- a/README.md +++ b/README.md @@ -34,13 +34,15 @@ optparse.define short=f long=file desc="The input file" variable=filename list=" The `optparse.build` function creates a header script and a configuration file in $HOME/.bash_completion.d/ based on the provided argument definitions. ```bash -$ optparse.build script_name +optparse.build script_name ``` ###5. Allow execution to the script and execute it. ##### See `sample_event_generate_completion.sh` for a demonstration. ###6. Source profile bash completion configuration, example: -source $HOME/.bash_completion.d/* +```bash +$ source $HOME/.bash_completion.d/* +``` ###7. In your command script( The script to parse arguments and generate completion ) source the optparse generated file to parse and evaluate arguments. From 474ea71ef7b655d21e3040368e62b0dbc09e7543 Mon Sep 17 00:00:00 2001 From: danyboy1104 Date: Wed, 26 Mar 2014 14:34:58 -0400 Subject: [PATCH 7/9] Readme.md modified --- README.md | 8 +++++--- sample_event_generate_completion.sh | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0e21063..5b09f9d 100644 --- a/README.md +++ b/README.md @@ -13,19 +13,21 @@ A wrapper that provides a clean and easy way to parse arguments and create Tab c Each argument to the script is defined with `optparse.define`, which specifies the option names, a short description, the variable it sets, the default value (if any) and a list of posible values (if any). ```bash -optparse.define short=f long=file desc="The input file" variable=filename +optparse.define short=n long=name desc="The event name" variable=name ``` Flags are defined in exactly the same way, but with an extra parameter `value` that is assigned to the variable. ```bash -optparse.define short=v long=verbose desc="Set flag for verbose mode" variable=verbose_mode value=true default=false +optparse.define short=c long=country desc="The event country" variable=country list="USA Canada Japan Brasil England" ``` ```bash -optparse.define short=f long=file desc="The input file" variable=filename list="string1 string2 string3" +optparse.define short=y long=year desc="The event year" variable=year list="2006 2010 2014 2020" ``` +Another way to pass a list is to assign a command output to list variable as follow. + ```bash optparse.define short=f long=file desc="The input file" variable=filename list="\$(my_command)" ``` diff --git a/sample_event_generate_completion.sh b/sample_event_generate_completion.sh index bbbc4b3..c7cf16c 100755 --- a/sample_event_generate_completion.sh +++ b/sample_event_generate_completion.sh @@ -6,7 +6,7 @@ source optparse.bash # Define options optparse.define short=n long=name desc="The event name" variable=name optparse.define short=c long=country desc="The event country" variable=country list="USA Canada Japan Brasil England" -optparse.define short=d long=year desc="The event year" variable=year list="2006 2010 2014 2020" +optparse.define short=y long=year desc="The event year" variable=year list="2006 2010 2014 2020" # Generate optparse and autocompletion scripts script_name="sample_event.sh" From 954a7f775737b93c14f251e5021ae9da4cdd734a Mon Sep 17 00:00:00 2001 From: danyboy1104 Date: Wed, 26 Mar 2014 14:54:07 -0400 Subject: [PATCH 8/9] Readme.md modified --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5b09f9d..6cd981e 100644 --- a/README.md +++ b/README.md @@ -26,10 +26,10 @@ optparse.define short=c long=country desc="The event country" variable=country l optparse.define short=y long=year desc="The event year" variable=year list="2006 2010 2014 2020" ``` -Another way to pass a list is to assign a command output to list variable as follow. +Another way to pass a completion list is to assign a command output to list variable as follow. ```bash -optparse.define short=f long=file desc="The input file" variable=filename list="\$(my_command)" +optparse.define short=y long=year desc="The event year" variable=year list="\$(my_command)" ``` ###5. Evaluate your arguments From 9266966e073ccb9bb6b2cd14c90465ac310a0109 Mon Sep 17 00:00:00 2001 From: danyboy1104 Date: Wed, 26 Mar 2014 14:56:13 -0400 Subject: [PATCH 9/9] Readme.md modified --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6cd981e..f138401 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ the list of posible arguments for an option to autocomplete. It could be a list 2. Add ```bash -`source /path/to/optparse.bash` +source /path/to/optparse.bash ``` to `~/.bashrc`