GithubHelp home page GithubHelp logo

replit / clui Goto Github PK

View Code? Open in Web Editor NEW
1.2K 1.2K 36.0 2.06 MB

CLUI is a collection of JavaScript libraries for building command-driven interfaces with context-aware autocomplete.

JavaScript 5.07% TypeScript 94.93%

clui's People

Contributors

0xflotus avatar akramhussein avatar ddybing avatar lunaroyster avatar mducharm avatar moudy avatar sareini avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

clui's Issues

Support for ordered (unnamed) arguments

First, let me say what a fantastic project this is and it's super easy to use ๐Ÿ™. Thank you for building and sharing.

I wanted to get your thoughts on how I could extend the @replit/clui-input library to support ordered unnamed arguments before and possibly after named arguments (not as important).

I've tried to read through all the code first and before I began tinkering wanted to check to my thinking - maybe it's already possible and you can point me in the right direction.

At the moment, the input structure is:

COMMAND [SUBCOMMAND] [--KEY [VALUE]]

Where:

  • There can be 0 to N subcommands to each (sub)command.
  • There can be 0 to N named arguments (--key value) per (sub)command which can be optional and typed.
  • Argument value must be preceded by a key.
  • Argument value not always required - if flag or
  • (Sub)commands and argument options can be resolved at runtime if they are async.

What I'd like to support in my application is:

COMMAND [SUBCOMMAND] [ARG] [--KEY [VALUE]]

The difference being the there is no requirement to identify the key and a free form value is valid for ARG (can be typed and required like named arguments), so it doesn't need to be from an options list.

In theory, this is already achievable if you:

  1. Don't provide options to the argument and enter them free-form, but still requires --KEY before.
  2. Treat ordered unnamed arguments as subcommands, but this means the values must all be known upfront. If the user wanted to enter COMMAND SUBCOMMAND foo, you would need to know foo in advance. I'd like to allow the user to enter a free form value (string, number, boolean) without specifying the key.
  3. Use ordered unnamed arguments after named arguments/the AST is exhausted and manually parse them after receiving an update from the library - loses the clean coupling with specific (sub)commands and forces user to always put named arguments before is a bit awkward when compared to normal CLI.

I imagine you already considered this and it's absence is due to the complexity of handling it, but I figured I'd check anyways.

At the top-level interface, one possible proposal would be to pass a config as such:

  const root: ICommand = {
    commands: {
      user: {
        args: {
          info: {
            type: 'boolean',
          },
          name: {
            type: 'string',
            key: false
          },
        },
      },
    },
  };

Where key: boolean indictes if it requires the --KEY before and if not, it is expected in the order it appears in the config. This however creates the situation of type: boolean + key: false = neither key or value are required.

Therefore, flags could be changed to an explicity property such as argType: 'flag' | 'named' | 'unnamed to combine them all.

Alternatively, it could be it's own property such as orderedArgs (for lack of better name):

  const root: ICommand = {
    commands: {
      user: {
        orderedArgs: {
          nickname: {
            type: 'string',
            required: true
          },
          email: {
          	type: 'string'
          }
        },
        args: {
          info: {
            type: 'boolean',
          },
          name: {
            type: 'string',
          },
        },
      },
    },
  };

From here it's possible the configuration object interfaces used would be almost identical to ICommandArgs except that IArg would not need options. However, since options is already optional, it could use the same interfaces outside the AST and for ASTNodeKind but have a different nodes:

export interface IOrderedArgKeyNode {
  kind: 'ARG_KEY';
  parent: IParamNode;
  token: IToken;
  name: string;
}

export interface IOrderedArgValueNode {
  kind: 'ARG_VALUE';
  parent: IParamNode;
  token: IToken;
}

export interface IOrderedArgNode {
  kind: 'ARG';
  ref: IArg;
  parent: ICmdNode;
  key: IArgKeyNode;
  value?: IArgValueNode;
}

If it would be clearer, interfaces could be more specific outside and inside the AST. For example:

export type ASTNodeKind =
  ...
  | 'ORDERED_ARG'
  | 'ORDERED_ARG_KEY'
  | 'ORDERED_ARG_VALUE'
  ...


export interface IOrderedArgs<D = any> {
  type?: OrderedArgTypeDef;
  required?: true;
  data?: D;
}

....etc

My intuition is taking the simplest path here of just augmenting the current IArg interface by setting a property to identify if its a flag, named or unnamed would be best and then the focus would be on the logic in parser.ts to change the logic for identifying arguments in all 3 styles.

Do you think this is something that is straightforward or would it fundamentally break how you've designed the parsing/AST/options system?

I'd appreciate any input you can provide and will glady create a PR if I can make progress.

I hope this makes, but if not please let me know.

Can I use this in a terminal emulator?

Sorry for the stupid question, but can I use this to write a command line application and render the suggestions in a terminal emulator?

If so, is there any example of it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.