Coder Social home page Coder Social logo

Comments (4)

UrsaDK avatar UrsaDK commented on May 24, 2024

Hi @pilak,

Using even the default getops inside a function is not exactly straight forward... But I'll see if I can add some test coverage for this use-case and workout what's going on with BASH_ARGV.

One thing that would really help is a simple example of how you're using getopts_long, as I'm not entirely sure what you mean by "nested function". Are you talking about a function within a function? Also, are you attempting to process arguments supplied to the script or to the function?

from getopts_long.

dodecatheon avatar dodecatheon commented on May 24, 2024

@UrsaDK : It is possible to use builtin getopts inside a function:

foo () {
  OPTIND=1
  while getopts ':ab:' OPT ; do
    case "$OPT" in
      a) a_stuff ;;
      b) b_stuff=$OPTARG ;;
      ...

However, with getopts_long, you can't do a similar loop, because, in the way you currently use OPTIND, you are looking through the arguments of the overall script, not the arguments of the function.

If you want to use getopts_long in a subcommand, for example passing the shifted arguments to foo using foo "$@", this is problematic.

from getopts_long.

dodecatheon avatar dodecatheon commented on May 24, 2024

Answering my own question, you already handle this.

foo () {
  OPTIND=1
  while getopts_long ':ab: alpha bravo:' OPT "$@"; do
    case "$OPT" in
      a|alpha) a_stuff=1 ;;
      b|bravo) b_stuff=$OPTARG ;;
      ...

from getopts_long.

UrsaDK avatar UrsaDK commented on May 24, 2024

Thanks for following this up with an answer @dodecatheon! 😄

I think, since this question came up twice now, I should probably add it to the README, and maybe even include it as a separate scenario in the tests. I'll add it to me TODO list!

For now, I'm going to resolve this as "Works as expected" since there isn't a difference between the way the original geopts and my getopts_long behave in this use case.

from getopts_long.

Related Issues (3)

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.