matejak / argbash

Bash argument parsing code generator
Other
1.39k stars 63 forks source link

VT100 control codes in strings and comments causing unmatched square bracket error #150

Closed emilheunecke closed 3 years ago

emilheunecke commented 3 years ago
Debugging process... I sometimes like to use [this](https://stackoverflow.com/a/12797512/14756965) technique to add comment to multiline scripts in bash. However, it seems like m4 considers the closing backtick ` and continuation character \ as part of the comment, so argbash throws an error: ``` /usr/bin/m4:stdin:36: ERROR: end of file in string autom4te: /usr/bin/m4 failed with exit status: 1 You seem to have an unmatched square bracket on line 6: # [ <-- needed because of Argbash Error during autom4te run, aborting! ``` I guess there isn't much there can be done since the syntax is incompatible with m4, but I wonder if anyone has a fix or some other way to accomplish the same thing?
emilheunecke commented 3 years ago
Debugging process... Actually, seems like the problem is more complex. The following is the offending code block: ```bash do_load () { local service=$1 local tar=$2 echo "Loading image from $_path_root/$service/images/$tar" evalfunc() { if [ -f load_log ] then cat load_log `# See http://www.cse.psu.edu/~kxc104/class/cmpen472/16s/hw/hw8/vt100ansi.htm`\ | sed "s/\x1B//g" `# Remove ESC`\ | sed 's/\[[12][ABK]/\n/g' `# Remove [1A, [2K and [1B`\ | sed "s/\r//g" `# Remove carriage return`\ | tail -1 fi } start_spinner_eval "evalfunc" faketty docker load -i $_path_root/$service/images/$tar >load_log stop_spinner rm load_log printf '\033[2K' # Clear current line } ``` However, even if I remove evalfunc, bash still throws an error. I have to remove BOTH evalfunc and the last printf line! Just removing one or the other doesn't work. Eg even just this breaks throws an unmatched square bracket error: ```bash do_load () { printf '\033[2K' # Clear current line } ``` Or this: ```bash do_load () { evalfunc() { if [ -f load_log ] then cat load_log `# See http://www.cse.psu.edu/~kxc104/class/cmpen472/16s/hw/hw8/vt100ansi.htm`\ | sed "s/\x1B//g" `# Remove ESC`\ | sed 's/\[[12][ABK]/\n/g' `# Remove [1A, [2K and [1B`\ | sed "s/\r//g" `# Remove carriage return`\ | tail -1 fi } } ``` But NOT this: ```bash do_load () { local service=$1 local tar=$2 echo "Loading image from $_path_root/$service/images/$tar" evalfunc() { if [ -f load_log ] then cat load_log fi } start_spinner_eval "evalfunc" faketty docker load -i $_path_root/$service/images/$tar >load_log stop_spinner rm load_log } ```
emilheunecke commented 3 years ago

Seems like it's actually the VT100 control codes in the strings and comment that are causing problems... This works:

do_load () {
  local service=$1
  local tar=$2
  echo "Loading image from $_path_root/$service/images/$tar"
  evalfunc() {
    if [ -f load_log ]
    then
      cat load_log `# See http://www.cse.psu.edu/~kxc104/class/cmpen472/16s/hw/hw8/vt100ansi.htm`\
      | sed "s/\x1B//g" `# Remove ESC`\
      | sed '' `# Remove `\
      | sed "s/\r//g" `# Remove carriage return`\
      | tail -1
    fi
  }
  start_spinner_eval "evalfunc"
  faketty docker load -i $_path_root/$service/images/$tar >load_log
  stop_spinner
  rm load_log
  printf '\033' # Clear current line
}

But only if I remove BOTH the last printf command's VT100 control code string content, the second sed command's VT100 control code string content AND the VT100 control codes in the comment next to the second sed command!

zach-data commented 3 years ago

I just hit this same issue, found this which seemed to work as documented: https://argbash.readthedocs.io/en/stable/index.html#limitations

Something like...

do_load () {
  printf '\033[2K' # Clear current line ... match square bracket: ]
}
emilheunecke commented 3 years ago

Closing since limitation is already documented.