arcanis/clipanion

Options cannot be used during object construction

Opened this issue · 2 comments

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).

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 🤔

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 🤔