Coder Social home page Coder Social logo

Comments (8)

DannyBen avatar DannyBen commented on September 25, 2024 1

but I think it would be nice to have a way to do this by convention.

I thought about this, but decided against.
In Rails, here is a concept of before and after filter, which is similar to what you are describing - since this is a "before" filter.

I found this convention to be more trouble than its worth, and it "gets in your way" more often than not.

The straight forward solution would be to add a library function (bashly add lib), and call it from any command file that needs it.

That said, your use case of using --debug to set -x indeed shows the weak spot of the decision to avoid some sort of "before" filter.

Let me sleep on it.

from bashly.

DannyBen avatar DannyBen commented on September 25, 2024 1

I found the issue where this was considered originally: #116

I wonder if adding optional before.sh and after.sh (similar to how initialize.sh works) should be reconsidered.
Meaning - if such files are found, they will be executed in the appropriate place, then you can simply do

# src/before.sh
if [[ ${args[--debug]} ]]; then
  set -x
fi

from bashly.

DannyBen avatar DannyBen commented on September 25, 2024 1

Thanks for raising this issue Wes.

This feature is now released in 1.0.2

Docs:

from bashly.

DannyBen avatar DannyBen commented on September 25, 2024

Thank you.

  1. Well, when you have subcommands, the root_command will never be called, this is why it is not there.
  2. As you noticed, the initialize is not the place - it is done before anything else.

Defining flags on the root, just makes these flags available in all subcommands. Note however, that input order matters:

$ <command> <root flags> <subcommand> <subcommand flags and args>

With this bashly.yml

name: cli
help: Sample application
version: 0.1.0

flags:
- long: --debug
  short: -d
  help: Enable debug mode

commands:
- name: download
  alias: d
  help: Download a file

  flags:
  - long: --force
    short: -f
    help: Overwrite existing files

you get:

$ ./cli --debug download --force
args:
- ${args[--debug]} = 1
- ${args[--force]} = 1

Does this help?
Was this not intuitive/clear enough in the docs? Let me know where I can improve the behavior or documentation.

from bashly.

westhomas avatar westhomas commented on September 25, 2024

Yes, that's clear from the docs, which are very good, btw.

I guess what I'm asking for is some way to run code after the arguments are initialized but before the command/sub-command is invoked.

For example, if I have the --debug flag on the root command, could there be some way to define a function that gets called?

      # :flag.case
      --debug)

        # :flag.case_no_arg
        args['--debug']=1
        shift
        # <========= need a function call right here
        ;;

My only alternative, is to check for ${args[--debug]} in every single sub-command's *_command.sh file. It works, but I think it would be nice to have a way to do this by convention.

from bashly.

DannyBen avatar DannyBen commented on September 25, 2024

This is now implemented in the edge version.

You can run bashly add hooks to add the two before/after hook files, and edit them as you see fit.

from bashly.

DannyBen avatar DannyBen commented on September 25, 2024

@westhomas were you able to test it?

from bashly.

westhomas avatar westhomas commented on September 25, 2024

Just tested it. It's working very nicely! Thank you!

from bashly.

Related Issues (20)

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.