Coder Social home page Coder Social logo

Comments (3)

pforret avatar pforret commented on August 25, 2024

Hi Sven,
what you want, is options that can be called > 1. This is not easy because of the way the options are parsed in bashew:
--config [value] is parsed into a $config variable, not an array.
A possible solution would be to allow --config:1 [value] --config:2 [value] syntax which would then be parsed into an array.

Me, I currently use comma-separated values e.g. --effect dark,blur,grain in pforret/splashmark, but you're right that it doesn't work well for file names.

It's a good suggestion, let me think on that one!

from bashew.

usrme avatar usrme commented on August 25, 2024

First off, kudos to you, @pforret, for creating such a wonderful project! The amount of effort you've put in really shines through.

Secondly, I'm quite possibly showing my own ignorance here as I don't know the intricacies of how Bashew does parameter parsing, but why, in this case, it isn't possible to simply define an array variable for a given parameter and append to it while going over the parameters?

Here's a simple working example outside of Bashew that on the face of it seems to work:

#!/usr/bin/env bash

script_name=$(basename "${BASH_SOURCE[0]}")
script_ver='0.0.1'

usage() {
  cat << EOF
Usage: ${script_name} [-h | --help] [-v | --verbose] [-V | --version]

Example script to demonstrate for optional list options.

Available options:

-h, --help           Print this help and exit
-v, --verbose        Print script debug info
-V, --version        Print version number and exit
-c, --config-file    Configuration file(s) to be consumed

Example: ${script_name} -c file1.yml -c file2.yml
Example: ${script_name} --config-file file1.yml --config-file file2.yml

EOF
  exit
}

config_files=()

while :; do
  case "$1" in
  -h | --help) usage ;;
  -v | --verbose)
    set -x
    export SHELLOPTS
    ;;
  -V | --version) echo "$script_ver" && exit ;;
  -c | --config-file)
    config_files+=("$2")
    shift
    ;;
  ?*)
    echo "Unexpected parameter: '${1}'"
    usage
    ;;
  *) break ;;
  esac
  shift
done

for file in "${config_files[@]}"; do
  echo "Working with file: ${file}"
done

from bashew.

pforret avatar pforret commented on August 25, 2024

Hi @usrme ,

you are exactly right that the implementation should be done with bash arrays - config_files+=("$2")

It's a bit more involved in bashew because I use just 1 function list_options() to define all flags/options/parameters and all usage info/variable initialisation/parsing is derived from there. I just implemented this in bashew version 1.13.0 (the 'normal' template)

Hi @spalberg ,

I did it the following way: there is now an option type 'list' that is by default an empty array and that you can call multiple times: --file /path/file1 --file /path/file2 and that you can use after automatic parsing as an bash array ${file[@]}

Thanks for the suggestion and I hope this makes bashew even better for your usage!

Peter

from bashew.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.