Skip to content

Commit

Permalink
Merge pull request #12 from rakus/new-func-check
Browse files Browse the repository at this point in the history
Change generated function check code
  • Loading branch information
rakus authored Oct 28, 2023
2 parents 39468ff + 132885b commit 751621a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
10 changes: 7 additions & 3 deletions doc/tutorial.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -433,15 +433,19 @@ To better understand how this works, see the sections <<CALLBACKS>> and <<SINGLE
Till now we used Parseargs to assign variables for the options found on the command line, but it is also able to work with shell functions.

When using functions, Parseargs also generates code to test for the existence of the function.
Assuming a function `set_out_file` should be used, the following code will always be generated (here for bash):
Assuming a function `set_out_file` should be used, it is always checked whether this function exists.

[%nowrap,source,bash]
----
typeset -f set_out_file >/dev/null 2>&1 || { echo >&2 "ERROR: Function set_out_file does not exist.";exit 127; };
# default
if ! LC_ALL=C command -V set_out_file 2>/dev/null | head -n1 | grep function >/dev/null; then echo >&2 "ERROR: Function 'set_out_file' does not exist."; exit 127; fi;
# with --shell bash, ksh or zsh
if ! typeset -f set_out_file >/dev/null 2>&1; then echo >&2 "ERROR: Function 'set_out_file' does not exist."; exit 127; fi;
----

This code will exit the calling script if the function does not exist.
This check is always done, whether the function is needed in the actually generated code or not.
The check is always done, whether the function is needed in the actually generated code or not.

When calling the callback the exit status of the function must be zero else the calling script is terminated with function exit code.
The code for this looks like this:
Expand Down
10 changes: 5 additions & 5 deletions src/shell_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ const SH_TEMPLATE : CodeTemplates = CodeTemplates {
assign_empty_array : "",
add_to_array : "",

check_function_exists : "LC_ALL=C type {NAME} 2>/dev/null | grep function >/dev/null || (echo >&2 \"ERROR: Function '{NAME}' does not exist.\";exit 1) || exit 127" ,
check_function_exists : "if ! LC_ALL=C command -V {NAME} 2>/dev/null | head -n1 | grep function >/dev/null; then echo >&2 \"ERROR: Function '{NAME}' does not exist.\"; exit 127; fi" ,
call_function : "{NAME} {VALUE} || exit $?",

set_args : "set -- {ARGS}",
Expand All @@ -241,7 +241,7 @@ const BASH_TEMPLATE : CodeTemplates = CodeTemplates {
assign_empty_array : "{NAME}=()",
add_to_array : "{NAME}+=({VALUE})",

check_function_exists: "typeset -f {NAME} >/dev/null 2>&1 || { echo >&2 \"ERROR: Function '{NAME}' does not exist.\";exit 127; }",
check_function_exists : "if ! typeset -f {NAME} >/dev/null 2>&1; then echo >&2 \"ERROR: Function '{NAME}' does not exist.\"; exit 127; fi" ,

// others from sh template
..SH_TEMPLATE
Expand Down Expand Up @@ -401,7 +401,7 @@ mod shell_template_test {
assert_eq!("func 'value' || exit $?", shell.format(&chunk));

let chunk = CodeChunk::CheckForFunction(var_name.clone());
assert_eq!("LC_ALL=C type func 2>/dev/null | grep function >/dev/null || (echo >&2 \"ERROR: Function 'func' does not exist.\";exit 1) || exit 127", shell.format(&chunk));
assert_eq!("if ! LC_ALL=C command -V func 2>/dev/null | head -n1 | grep function >/dev/null; then echo >&2 \"ERROR: Function 'func' does not exist.\"; exit 127; fi", shell.format(&chunk));

let chunk = CodeChunk::SetArgs(vec![
"one".to_string(),
Expand Down Expand Up @@ -464,7 +464,7 @@ mod shell_template_test {
assert_eq!("func 'value' || exit $?", shell.format(&chunk));

let chunk = CodeChunk::CheckForFunction(var_name);
assert_eq!("typeset -f func >/dev/null 2>&1 || { echo >&2 \"ERROR: Function 'func' does not exist.\";exit 127; }", shell.format(&chunk));
assert_eq!("if ! typeset -f func >/dev/null 2>&1; then echo >&2 \"ERROR: Function 'func' does not exist.\"; exit 127; fi", shell.format(&chunk));

let chunk = CodeChunk::SetArgs(vec![
"one".to_string(),
Expand Down Expand Up @@ -515,7 +515,7 @@ mod shell_template_test {
assert_eq!("func 'value' || exit $?", shell.format(&chunk));

let chunk = CodeChunk::CheckForFunction(var_name);
assert_eq!("typeset -f func >/dev/null 2>&1 || { echo >&2 \"ERROR: Function 'func' does not exist.\";exit 127; }", shell.format(&chunk));
assert_eq!("if ! typeset -f func >/dev/null 2>&1; then echo >&2 \"ERROR: Function 'func' does not exist.\"; exit 127; fi", shell.format(&chunk));

let chunk = CodeChunk::SetArgs(vec![
"one".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion tests/code_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod exec;

/// Create function check code for given function name for standard shell
fn sh_func_check(func_name: &str) -> String {
format!("LC_ALL=C type {func_name} 2>/dev/null | grep function >/dev/null || (echo >&2 \"ERROR: Function '{func_name}' does not exist.\";exit 1) || exit 127;")
format!("if ! LC_ALL=C command -V {func_name} 2>/dev/null | head -n1 | grep function >/dev/null; then echo >&2 \"ERROR: Function '{func_name}' does not exist.\"; exit 127; fi;")
}

#[test]
Expand Down

0 comments on commit 751621a

Please sign in to comment.