arcanis / clipanion

Type-safe CLI library / framework with no runtime dependencies
https://mael.dev/clipanion/
1.1k stars 61 forks source link

Options cannot be used during object construction #129

Open swandir opened 2 years ago

swandir commented 2 years ago

Options appear as regular public class fields but do not function as such during class instance construction.

class DefaultCommand extends Command {
  static paths = [Command.Default];

  s = Option.String('-s', {
    required: true,
  });

  uppercase = this.s.toUpperCase(); // TypeError: this.s.toUpperCase is not a function

  async execute() {}
}

https://stackblitz.com/edit/node-gbmupy?file=cli.ts

This behavior is understandable given the way options are declared, but still might take the user by surprise. Especially when it fails silently (optional property usage, for example).

arcanis commented 2 years ago

Indeed; we could avoid that by making Option.String & friends return a Proxy that would "buffer" all manipulations, but it'd be quite overkill imo. I'm not sure what else we can do 🤔

swandir commented 1 year ago

Hmm, could options return the actual value and register themselves for command routing as a side-effect? It would require to delay command construction until after cli.run is called though 🤔