Coder Social home page Coder Social logo

Support subcommands about meow HOT 9 OPEN

sindresorhus avatar sindresorhus commented on August 21, 2024 19
Support subcommands

from meow.

Comments (9)

MartinMuzatko avatar MartinMuzatko commented on August 21, 2024 7

I have written a script to allow subcommands in this structure:
https://gist.github.com/MartinMuzatko/2922add0f84535a44f98a8e8fa729efa

from meow.

sindresorhus avatar sindresorhus commented on August 21, 2024 2

Do you have any ideas/criticism to this approach?

Yeah, I'm fine with that approach, but it should be called commands.

from meow.

gtarsia avatar gtarsia commented on August 21, 2024 1

tldr: commands property of type Array<String> that fail if the command is not found, and possibly a level property of type Number that indicates the level of the commands.

I like meow because it's useful yet simple, but most implementations of subcommands are ridiculous, so I favor this change as long as user control and simplicity are prioritized.

I also think that the property should be called commands instead of subcommands, it's shorter and it's still correct. For example, git clone, git add and git push are all git commands.

A more simple way is making meow to expect the commands property to be an Array<String>,
It could even have suggestions in case you misspell a command (like git does).

import fetch from './cli-fetch'
import sort from './cli-sort'

const options = { fetch, sort }
const { command, flags } = meow({
  commands: Object.keys(options),
  ...
})
options[command](flags)
// each cli-<action> module can use meow in turn too
$ foo michael
foo: 'michael' is not a foo command. See foo --help
The most similar commands are
    fetch
    sort

This way meow doesn't concern with calling functions and deciding which params to pass to these.
The Object.keys usage could become a standard for deciding which command to use.

A problem with my solution is that sometimes commands are the second level, for example the connect in docker network connect. So the level option should decide the index of input to pick the command.

// fictional-docker-network.js
meow({
  level: 1,
  commands: ['connect', 'create', 'disconnect', 'inspect', 'ls', 'prune', 'rm']
})

It's simple but I'm not too sold on it. This is open to criticism and I'm willing to push a PR in any case.

Regarding @muuvmuuv's solution, I like that it doesn't support nesting of commands but it still has a couple of challenges that I think disqualifies it:

  1. It doesn't tell you which command should it run.
  2. It takes some control away by calling a function you provide it with.

from meow.

skoshx avatar skoshx commented on August 21, 2024 1

Any work being done on this feature? I like @muuvmuuv 's idea of having meow instances as subcommands, I think it's the most flexible, most simple & makes it easy to separate subcommands into different files for a cleaner and more maintainable codebase.

@sindresorhus Do you have any ideas/criticism to this approach? Was thinking that if no one is working on this that I could do it as soon as I have time.

from meow.

aaronccasanova avatar aaronccasanova commented on August 21, 2024 1

Hello 👋 I've been experimenting with an alternative approach for adding support for subcommands and would love to get folks thoughts and opinions on the API!

Here is my WIP

from meow.

muuvmuuv avatar muuvmuuv commented on August 21, 2024

Just an idea but I thought about we could do it similar to Listr

#!/usr/bin/env node
"use strict";
const meow = require("meow");
const foo = require(".");

const flags = {
  rainbow: {
    type: "boolean",
    alias: "r"
  }
}

const subCommands = {
  horseman: (flags /* parsed flags from parent */) => {
    return meow(
      `
      Usage
        $ horseman
      .....
    `,
      {
        name: {
          type: "string",
          default: "The Horseman"
        }
      }
    );
  }
}

const cli = meow(
  `
  Usage
    $ foo <input>

  Options
    --rainbow, -r  Include a rainbow

  Examples
    $ foo unicorns --rainbow
    🌈 unicorns 🌈
`,
  {
    flags,
    subCommands
  }
);

foo(cli.input[0], cli.flags);

from meow.

skoshx avatar skoshx commented on August 21, 2024

I agree that it should be called commands, I'll get on it as soon as possible, unless someone beats me to the punch :D

from meow.

detj avatar detj commented on August 21, 2024

My attempt without changing anything in meow. Uses Dynamic Imports to load the additional command CLIs.

https://github.com/detj/meow-subcommands

I'm sure, there might be issues I'm unaware of. Feel free to offer suggestions.

from meow.

voxpelli avatar voxpelli commented on August 21, 2024

I solved it like this in the @SocketDev CLI: https://github.com/SocketDev/socket-cli-js/blob/f45cbd408c0575dfe205cf968596f4506f15f44d/lib/utils/meow-with-subcommands.js#L33

Planned to extract it to its own module and might still do even though I'm no longer with Socket

from meow.

Related Issues (20)

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.