Comments (9)
I have written a script to allow subcommands in this structure:
https://gist.github.com/MartinMuzatko/2922add0f84535a44f98a8e8fa729efa
from meow.
Do you have any ideas/criticism to this approach?
Yeah, I'm fine with that approach, but it should be called commands
.
from meow.
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:
- It doesn't tell you which command should it run.
- It takes some control away by calling a function you provide it with.
from meow.
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.
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.
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.
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.
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.
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)
- Use Node.js builtin HOT 4
- What is the test coverage? HOT 5
- `allowUnknownFlags: false` prevents aliases for `help` and `version`. HOT 3
- Remove `trim-newlines` dependency HOT 4
- ExperimentalWarning when using Meow with modules HOT 4
- Is it possible to make input required? HOT 3
- Improve importMeta message -`meow - The 'importMeta' option is required. Its value must be 'import.meta' at meow` HOT 7
- Expose package.json version as property HOT 4
- Allow single line help messages HOT 11
- Refactor HOT 6
- Question: huge bundle size? HOT 2
- Choices? HOT 2
- flag.default values validation is faulty
- Feature Proposal - Disable Indent HOT 12
- `meow(...).flags` is `any` in 12.1.0 HOT 1
- Add "types" field in package.json HOT 2
- typescript definitions stopped working HOT 2
- String value converted to Num HOT 2
- Remove hard-rejection
- ESLint `import/no-unresolved` false positive when module is imported HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from meow.