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

Allow option to set global header/banner for all commands #1578

Closed
tlylt opened this issue Aug 9, 2021 · 2 comments
Closed

Allow option to set global header/banner for all commands #1578

tlylt opened this issue Aug 9, 2021 · 2 comments

Comments

@tlylt
Copy link

tlylt commented Aug 9, 2021

Hello,

I am hoping to add a common banner that will be displayed as the first thing in the output when running any command. Similar to #578, but for general commands and not for the help command. (Is Life cycle hooks the solution to my question?)

So for example a command output could become:

=====
MyProgram version 1.2.3
=====
Run successfully!

I have searched through the issues and found out that support has been added to customize help text and make it possible to do the above for help commands, via addHelpText() or configureOutput(). Although a solution has been suggested in this comment: #1272 (comment), would it be possible to add this functionality as part of commander.js?

So I imagine something like:

program.banner("Custom Banner")

Thanks!

@shadowspawn
Copy link
Collaborator

As you suggested, I think a "preAction" hook might be a solution for your case.

program.hook("preAction", () => console.log('BEFORE'));

program.command('foo')
  .action(() => console.log('hello'));

program.parse();
% node index.js foo 
BEFORE
hello

% node index.js foo --silly   
error: unknown option '--silly'

@tlylt
Copy link
Author

tlylt commented Aug 9, 2021

As you suggested, I think a "preAction" hook might be a solution for your case.

program.hook("preAction", () => console.log('BEFORE'));

program.command('foo')
  .action(() => console.log('hello'));

program.parse();
% node index.js foo 
BEFORE
hello

% node index.js foo --silly   
error: unknown option '--silly'

Thank you for the speedy reply! After playing around with it, I think it would serve my purpose just right 😄

@tlylt tlylt closed this as completed Aug 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants