arg-parse-gen.rb
def generate_bash_function
    bash_function = []
    emit = lambda {|code| bash_function << code}
    emit['declare -A arguments']
    emit['function argument_parser() {']
    emit['  local current_argument']
    emit['  local multi_arg']
    emit['  local function_arguments']
    emit['  function_arguments=("$@")']
    emit['  for arg in "${function_arguments[@]}"; do']
    @arguments.each do |argument_name, argument_options|
      argument_options.generate_bash_sequence.each {|line| emit[line]}
    end
    @arguments.each do |argument_name, argument_options|
      argument_options.generate_validation_code.each {|line| emit[line]}
    end
    emit['    if [[ ! "$current_argument" ]]; then']
    emit['      echo current_argument is not set.']
    emit['      echo Make sure the first argument is actually specified with --[ARGUMENT NAME]']
    emit['      exit 1']
    emit['    fi']
    emit['    if [[ $multi_arg ]]; then']
    emit['      if [[ ! ${arguments["$current_argument"]} ]]; then']
    emit['        arguments["$current_argument"]=""']
    emit['      fi']
    emit['      arguments["$current_argument"]+="$arg "']
    emit['      continue']
    emit['    fi']
    emit['    arguments["$current_argument"]="$arg"']
    emit['    current_argument=""']
    emit['    multi_arg=false']
    emit['  done']
    @arguments.each do |argument_name, argument_options|
      if argument_options.required?
        emit["  if [[ ! arguments[\"#{argument_name}\"] ]]; then"]
        emit["    echo Went through the entire argument array and did not find a required parameter: \\'--#{argument_name}\\'."]
        emit['    exit 1']
        emit['  fi']
      end
    end
    emit['}']
    bash_function.join("\n")
  end
##
# Assume we have associative arrays, i.e. hash maps, when generating the parsing
# function.
Assume we have associative arrays, i.e. hash maps, when generating the parsing
function.
Assume we have associative arrays i . e . hash maps when generating the parsing function .
def generate_bash_function
    bash_function = []
    emit = lambda {|code| bash_function << code}
    emit['declare -A arguments']
    emit['function argument_parser() {']
    emit['  local current_argument']
    emit['  local multi_arg']
    emit['  local function_arguments']
    emit['  function_arguments=("$@")']
    emit['  for arg in "${function_arguments[@]}"; do']
    @arguments.each do |argument_name, argument_options|
      argument_options.generate_bash_sequence.each {|line| emit[line]}
    end
    @arguments.each do |argument_name, argument_options|
      argument_options.generate_validation_code.each {|line| emit[line]}
    end
    emit['    if [[ ! "$current_argument" ]]; then']
    emit['      echo current_argument is not set.']
    emit['      echo Make sure the first argument is actually specified with --[ARGUMENT NAME]']
    emit['      exit 1']
    emit['    fi']
    emit['    if [[ $multi_arg ]]; then']
    emit['      if [[ ! ${arguments["$current_argument"]} ]]; then']
    emit['        arguments["$current_argument"]=""']
    emit['      fi']
    emit['      arguments["$current_argument"]+="$arg "']
    emit['      continue']
    emit['    fi']
    emit['    arguments["$current_argument"]="$arg"']
    emit['    current_argument=""']
    emit['    multi_arg=false']
    emit['  done']
    @arguments.each do |argument_name, argument_options|
      if argument_options.required?
        emit["  if [[ ! arguments[\"#{argument_name}\"] ]]; then"]
        emit["    echo Went through the entire argument array and did not find a required parameter: \\'--#{argument_name}\\'."]
        emit['    exit 1']
        emit['  fi']
      end
    end
    emit['}']
    bash_function.join("\n")
  end
def generate_bash_function bash_function = [ ] emit = lambda { | code | bash_function << code } emit [ 'declare -A arguments' ] emit [ 'function argument_parser() {' ] emit [ '  local current_argument' ] emit [ '  local multi_arg' ] emit [ '  local function_arguments' ] emit [ '  function_arguments=("$@")' ] emit [ '  for arg in "${function_arguments[@]}"; do' ] @arguments . each do | argument_name , argument_options | argument_options . generate_bash_sequence . each { | line | emit [ line ] } end @arguments . each do | argument_name , argument_options | argument_options . generate_validation_code . each { | line | emit [ line ] } end emit [ '    if [[ ! "$current_argument" ]]; then' ] emit [ '      echo current_argument is not set.' ] emit [ '      echo Make sure the first argument is actually specified with --[ARGUMENT NAME]' ] emit [ '      exit 1' ] emit [ '    fi' ] emit [ '    if [[ $multi_arg ]]; then' ] emit [ '      if [[ ! ${arguments["$current_argument"]} ]]; then' ] emit [ '        arguments["$current_argument"]=""' ] emit [ '      fi' ] emit [ '      arguments["$current_argument"]+="$arg "' ] emit [ '      continue' ] emit [ '    fi' ] emit [ '    arguments["$current_argument"]="$arg"' ] emit [ '    current_argument=""' ] emit [ '    multi_arg=false' ] emit [ '  done' ] @arguments . each do | argument_name , argument_options | if argument_options . required? emit [ "  if [[ ! arguments[\"#{argument_name}\"] ]]; then" ] emit [ "    echo Went through the entire argument array and did not find a required parameter: \\'--#{argument_name}\\'." ] emit [ '    exit 1' ] emit [ '  fi' ] end end emit [ '}' ] bash_function . join ( "\n" ) end
Assume we have associative arrays, i.e.
Assume we have associative arrays i . e .