Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax error on parseArger generated script #128

Open
DimitriGilbert opened this issue Jan 23, 2025 · 3 comments
Open

Syntax error on parseArger generated script #128

DimitriGilbert opened this issue Jan 23, 2025 · 3 comments

Comments

@DimitriGilbert
Copy link

I wanted to try your compiler on the script generated by my tool parseArger but unfortunatly, I get this error when trying to build :
Error: syntax error: expected ]to close conditional expression, found-. (line: 8, column: 39)

The command I use to build :
docker run -v .:/bunster ghcr.io/yassinebenaid/bunster:latest bunster build hello-world.sh -o hi

and the bash script I try to compile

#!/bin/bash
# @parseArger-begin
# @parseArger-help "Hi" --option "help" --short-option "h"
# @parseArger-verbose --option "verbose" --level "0" --quiet-option "quiet"
_has_colors=0
if [ -t 1 ]; then # Check if stdout is a terminal
	ncolors=$(tput colors 2>/dev/null)
	if [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
		_has_colors=1
	fi
fi
# @parseArger-declarations
# @parseArger pos person "who to greet"
# @parseArger opt greet "how to greet" --default-value "Hello"
# @parseArger flag exclam "!"
# @parseArger-declarations-end

# @parseArger-utils
_helpHasBeenPrinted=1;
_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)";
# @parseArger-utils-end

# @parseArger-parsing

__cli_arg_count=$#;

die()
{
	local _ret=1
    if [[ -n "$2" ]] && [[ "$2" =~ ^[0-9]+$ ]]; then
   	_ret="$2"
    fi
	test "${_PRINT_HELP:-no}" = yes && print_help >&2
	log "$1" -3 >&2
	exit "${_ret}"
}


begins_with_short_option()
{
	local first_option all_short_options=''
	first_option="${1:0:1}"
	test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
}

# POSITIONALS ARGUMENTS
_positionals=();
_optional_positionals=();
_arg_person="";
# OPTIONALS ARGUMENTS
_arg_greet="Hello"
# FLAGS
_arg_exclam="off"
# NESTED
_verbose_level="0";



print_help()
{
	_triggerSCHelp=1;

	if [[ "$_helpHasBeenPrinted" == "1" ]]; then
		_helpHasBeenPrinted=0;
		echo -e "Hi:"
	echo -e "	person: who to greet"
	echo -e "	--greet <greet>: how to greet [default: ' Hello ']"
	echo -e "	--exclam|--no-exclam: !"
	echo -e "Usage :
	$0 <person> [--greet <value>] [--[no-]exclam]";
	fi

}

log() {
	local _arg_msg="${1}";
	local _arg_level="${2:-0}";
	if [ "${_arg_level}" -le "${_verbose_level}" ]; then
		case "$_arg_level" in
			-3)
				_arg_COLOR="�[0;31m";
				;;
			-2)
				_arg_COLOR="�[0;33m";
				;;
			-1)
				_arg_COLOR="�[1;33m";
				;;
			1)
				_arg_COLOR="�[0;32m";
				;;
			2)
				_arg_COLOR="�[1;36m";
				;;
			3)
				_arg_COLOR="�[0;36m";
				;;
			*)
				_arg_COLOR="�[0m";
				;;
		esac
		if [ "${_has_colors}" == "1" ]; then
			echo -e "${_arg_COLOR}${_arg_msg}�[0m";
		else
			echo "${_arg_msg}";
		fi
	fi
}

parse_commandline()
{
	_positionals_count=0
	while test $# -gt 0
	do
		_key="$1"
		case "$_key" in
			--greet)
				test $# -lt 2 && die "Missing value for the option: '$_key'" 1
				_arg_greet="$2"
				shift
				;;
			--greet=*)
				_arg_greet="${_key##--greet=}"
				;;
			
			--exclam)
				_arg_exclam="on"
				;;
			--no-exclam)
				_arg_exclam="off"
				;;
			-h|--help)
				print_help;
				exit 0;
				;;
			-h*)
				print_help;
				exit 0;
				;;
			--verbose)
					if [ $# -lt 2 ];then
						_verbose_level="$((_verbose_level + 1))";
					else
						_verbose_level="$2";
						shift;
					fi
					;;
				--quiet)
					if [ $# -lt 2 ];then
						_verbose_level="$((_verbose_level - 1))";
					else
						_verbose_level="-$2";
						shift;
					fi
					;;
				
				*)
				_last_positional="$1"
				_positionals+=("$_last_positional")
				_positionals_count=$((_positionals_count + 1))
				;;
		esac
		shift
	done
}


handle_passed_args_count()
{
	local _required_args_string="person"
	if [ "${_positionals_count}" -gt 1 ] && [ "$_helpHasBeenPrinted" == "1" ];then
		_PRINT_HELP=yes die "FATAL ERROR: There were spurious positional arguments --- we expect at most 1 (namely: $_required_args_string), but got ${_positionals_count} (the last one was: '${_last_positional}').
	${_positionals[*]}" 1
	fi
	if [ "${_positionals_count}" -lt 1 ] && [ "$_helpHasBeenPrinted" == "1" ];then
		_PRINT_HELP=yes die "FATAL ERROR: Not enough positional arguments - we require at least 1 (namely: $_required_args_string), but got only ${_positionals_count}.
	${_positionals[*]}" 1;
	fi
}


assign_positional_args()
{
	local _positional_name _shift_for=$1;
	_positional_names="_arg_person ";
	shift "$_shift_for"
	for _positional_name in ${_positional_names};do
		test $# -gt 0 || break;
		eval "if [ \"\$_one_of${_positional_name}\" != \"\" ];then [[ \"\${_one_of${_positional_name}[*]}\" =~ \"\${1}\" ]];fi" || die "${_positional_name} must be one of: $(eval "echo \"\${_one_of${_positional_name}[*]}\"")" 1;
		eval "$_positional_name=\${1}" || die "Error during argument parsing, possibly an ParseArger bug." 1;
		shift;
	done
}

print_debug()
{
	print_help
	# shellcheck disable=SC2145
	echo "DEBUG: $0 $@";
	
	echo -e "	person: ${_arg_person}";
	echo -e "	greet: ${_arg_greet}";
	echo -e "	exclam: ${_arg_exclam}";

}


on_interrupt() {
	die Process aborted! 130;
}


parse_commandline "$@";
handle_passed_args_count;
assign_positional_args 1 "${_positionals[@]}";
trap on_interrupt INT;



# @parseArger-parsing-end
# print_debug "$@"
# @parseArger-end

output="$_arg_greet, $_arg_person";
if [ "$_arg_exclam" == "on" ]; then
    output+=" !";
fi
echo "$output"

to try it ./hello-world.sh Yassine --greet "Salam alaykum" --exclam

and if you want to generate the parsing code with parseArger

parseArger generate --output hello-world.sh --help-message "Hi" \
  --pos 'person "who to greet"' \
  --opt 'greet "how to greet" --default-value "Hello"' \
  --flag 'exclam "!"'

I am concious it might be very much an edge case but I like the idea of being able to compile my bash scripts !

Are there pit falls I should avoid for Bunster to work best ?

@yassinebenaid
Copy link
Owner

There is nothing wrong with your script or the commands you ran.

The issue is that bunster is in its early stages of development. Not all features in your script are supported.

To learn more about what features are supported by bunster, please read this document.

However, This is temporary. I'm doing my best to add support for all remaining features.

@DimitriGilbert
Copy link
Author

No worries, you get my ⭐ , I want to see where this is going :)

best of luck in this

@banyet1
Copy link

banyet1 commented Jan 25, 2025

Same issue here, looking forward get solved soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants