Skip to content

Add support for -abc, -a=arg and --flag=arg notation #103

@DannyBen

Description

@DannyBen

Support for these flag notations can probably be achieved easily if we add a normalization function before calling parse_requirements:

https://github.com/DannyBen/bashly/blob/7592d5e07ecf1cf4f910352fbab19804994d8629/lib/bashly/views/command/run.erb#L5

Changing it to something like this:

normalize_input   # this function will set a new array named $input
parse_requirements "${input[@]}"

Possible implementation

#!/usr/bin/env bash
normalize_input() {
  local arg flags

  while [[ $# -gt 0 ]]; do
    arg="$1"
    if [[ $arg =~ ^(--[^=]+)=(.+)$ ]]; then
      input+=("${BASH_REMATCH[1]}")
      input+=("${BASH_REMATCH[2]}")
    elif [[ $arg =~ ^(-[^=])=(.+)$ ]]; then
      input+=("${BASH_REMATCH[1]}")
      input+=("${BASH_REMATCH[2]}")
    elif [[ $arg =~ ^-([^-].+)$ ]]; then
      flags="${BASH_REMATCH[1]}"
      for (( i=0 ; i < ${#flags} ; i++ )); do
        input+=("-${flags:i:1}")
      done
    else
      input+=("$arg")
    fi    
    
    shift
  done
}

declare -a input
normalize_input "$@"
for value in "${input[@]}"; do
  echo ": $value"
done

Output

$ ./test.sh -a -b arg -cde -f=arg --flag=arg -a=b=c --flag=anything=goes
: -a
: -b
: arg
: -c
: -d
: -e
: -f
: arg
: --flag
: arg
: -a
: b=c
: --flag
: anything=goes

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions