un-clever / cli-library

The unclever CLI library. Built for re-use
MIT License
0 stars 1 forks source link

make Help consistently stdout or stderr, involves flagparser design and exceptions. #28

Open bradobro opened 2 hours ago

bradobro commented 2 hours ago

I think the best way, honoring the https://en.wikipedia.org/wiki/Semipredicate_problem, is like this:

  1. When help (or any sort of documentation) is explicitly requested, it should go to stdout. It's the legitimate output of the program.
  2. When help is shown because of an invocation (command-line) error, it should go to stderr, because it isn't the legitimate output of the program and shouldn't be interpreted as such.

Currently:

The sw design question is, I think, how best for the flag parser to signal exceptional behavior. Current cases:

We could add an extra return param: {flags, args, exitCode?: number / success: boolean}.

We could pass back smarter exceptions (that have status code and can introduce exit or other behavior (like show help and exit).

Exceptions are another, non-sequential channel for handling the semipredicate problem. Go hates them; C# finds them revolutionary. Rust, Go, and Zig go back to multiple returns. JavaScript uses them, with some haters. Let's not make this a language debate.

What sort of exceptional / extra behavior can I imagine?

Proposal:

ATM, I think the cleanest design is:

bradobro commented 2 hours ago

Another angle though, if we want a clean exit, it'll likely come before the flags value is complete--still Partial and likely empty. We could:

  1. cast it to be full (though it's not) and add the rule that we NEVER pass such flags on,
  2. make flags optional, then always have to check
  3. make the parser return a union type, and always have to check
  4. use a special exception new JustExit(code: number)