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

move output information to printer #779

Closed
wants to merge 6 commits into from
Closed

move output information to printer #779

wants to merge 6 commits into from

Conversation

cravler
Copy link
Contributor

@cravler cravler commented Mar 15, 2018

const { Command, Printer } = require('commander');

class CustomPrinter extends Printer {
 
    error(msg, ...args) {
        super.error(this.format(msg), ...args);
    }
 
    outputHelp(program) {
        return this.format(super.outputHelp(program));
    }
 
    format(str) {
        return [
            '\n', str.replace(/^/gm, '  '), '\n'
        ].join('');
    }
 
}

const program = new Command('example', new CustomPrinter());

program.version('0.0.1')
    .command('setup [env]')
    .description('application simple description', {
        env: 'env value'
    })
    .option('-f, --foo', 'enable some foo')
    .action(function(env, options) {});

program.parse(process.argv);
if (!program.args.length) program.help();

@cravler
Copy link
Contributor Author

cravler commented Mar 28, 2018

any comments?

@cravler
Copy link
Contributor Author

cravler commented May 2, 2018

Is this repository supported?

@abetomo
Copy link
Collaborator

abetomo commented Aug 9, 2018

The test has failed.
Could you check it?

@cravler
Copy link
Contributor Author

cravler commented Jan 17, 2019

any news?

@shadowspawn
Copy link
Collaborator

shadowspawn commented Apr 30, 2019

@cravler
What problem are you solving with this Pull Request?
Are there open Issues that this will resolve?

Or is this primarily an object separation suggestion, rather than a functional suggestion?

Edit: that probably sounded a bit blunt, sorry! I am working through backlog of Pull Requests making sure I understand them, so asking lots of questions.

@cravler
Copy link
Contributor Author

cravler commented May 2, 2019

This separation adds ability to write custom printers without touching the default. In my case I have Colorized Printer

@shadowspawn
Copy link
Collaborator

Thanks @cravler. This is quite interesting, but changes a lot of code. Offering customisation by inheritance is also more challenging for future maintenance than a callback.

I don't wish to add this at the moment. However we have a variety of requests around help and output. This may get revisited in the future if it can be used to resolve more issues.

Thank you for your contributions.

@shadowspawn shadowspawn closed this May 2, 2019
@shadowspawn shadowspawn mentioned this pull request Jul 19, 2020
Melissa0x1f992 added a commit to Melissa0x1f992/commander.js that referenced this pull request Oct 13, 2020
stdout isn't available to my user.  I have a custom method of displaying text output to the user.  I still want to make use of the full Commander help suite.  Disabling the built-in help commands and writing wrappers becomes an increasingly complicated task when you want to make use of the entire help suite.  This PR will solve that problem by removing the assumption that all output is to `process.stdout`.  A user can instead pass a writeable stream that will replace the default stdout stream.  In this way, anyone can output command results in any way that works for their use case by defining their own writeable stream.

This is a more elegant solution to the problem in tj#1370.  Instead of using `helpOption(false)` and writing my own help option, I can use Commander's help option, and handle the output with my own writeable stream.  This is a less invasive solution than tj#779, as it leaves the implementation of the writeable stream to the user.

Suggested changelog: "allow overriding the output from default (`process.stdout`) to any `stream.Writeable`"

Things to note:
* I've intentionally not updated the README or the examples/, as this is a WIP PR, and I'd like to know if this idea will be accepted before documenting it that far.
* This adds a dependency on `stream.Writeable`, but that's built into Node, so I think that's fine
* `process.stdout` is a `stream.Writeable`, but it's also a `tty.WriteStream`.  Only the latter has the `.columns` field in its API.  All of Commander's uses of that field are protected by a default if `.columns` is Falsey.  So for that reason I've kept it simple with the understanding that if someone wants a non-default column width in their custom stream, they can specify that field themself.  An alternative would be to create a type that's just a `stream.Writeable` + `.columns`.  I think this alternative adds unneeded complexity, so I didn't go for it.  However, it has the pro of not relying on the existing `.columns || 80` in the code to prevent undefined behavior.
@Melissa0x1f992 Melissa0x1f992 mentioned this pull request Oct 14, 2020
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

Successfully merging this pull request may close these issues.

3 participants