Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multiple actions #209

Open
1 of 2 tasks
chriscalo opened this issue May 5, 2021 · 0 comments
Open
1 of 2 tasks

Support for multiple actions #209

chriscalo opened this issue May 5, 2021 · 0 comments
Labels

Comments

@chriscalo
Copy link
Contributor

chriscalo commented May 5, 2021

Is your feature request related to a problem? Please describe.
It's at times useful to have multiple action functions run in succession. This makes it easier to keep things DRY when there's reusable logic and can help decompose large functions into smaller composable pieces.

Describe the solution you'd like
Much like Express, it seems reasonable to support multiple action functions with some mechanism to continue or not.

#!/usr/bin/env node
const { program } = require("@caporal/core");

program.action(({ args, options, next }) => {
  if (signedIn()) {
    next();
  } else {
    throw new Error("You must sign in first");
  }
});

program.action(({ args, options }) => {
  console.log("Hello, world!");
});

program.run();

Describe alternatives you've considered
There's a workaround that looks like the following:

#!/usr/bin/env node
const { program } = require("@caporal/core");

program.action({ args, options } => {
  signinRequired();
  console.log("Hello, world!");
});

program.run();

function signinRequired() {
  if (signedIn()) {
    next();
  } else {
    throw new Error("You must sign in first");
  }
}

This works, but things could be tidier and simpler with separate functions. One problem the workaround requires dealing with is passing along arguments from the action handler ({ args, options }) to the signinRequired() function if they're needed.

Additional context
None.

Would you be able to work on it and provide a pull request ?

  • Yes
  • No
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants