Comments (9)
or mycommand.arguments.groups
?
from cri.
I need a decent example of where this would be useful. I remember you mentioning a command similar to add-tags
:
add-tags foo.md bar.md clojure scala
The foo.md
and bar.md
arguments clearly (to a human, at least) are filenames, while clojure
and scala
are tags. For the add-tags
example, would you require users to separate filenames and tags with --
?
add-tags foo.md bar.md -- clojure scala
The implementation of add-tags
would then check the number of argument groups, and if it is not equal to 2, show an error message. Is that right?
from cri.
Okay, I've got a concrete example :)
Forematter has an "add" command, and it works like this:
forematter add <field> <value> [<value>...] <file> [<file>...]
Note that it accepts multiple values to add, and multiple files. Most of the time it's pretty easy to guess which are values and which are files...
$ forematter add tags bacon sausage ham content/articles/*
Because bacon
, sausage
and ham
aren't files, we can fairly reliably guess the appropriate argument partition. But in some cases, it can be ambiguous. For example:
$ forematter add tags ruby Gemfile content/building-a-ruby-gem.md
In this case, I could be adding the tags ruby
and Gemfile
to my "building a ruby gem" post, or I could be adding the tag ruby
to both my Gemfile and my "building a ruby gem" post. So in this case, the user could disambiguate:
$ forematter add tags ruby -- Gemfile content/building-a-ruby-gem.md
Good prior art for this is found in many of Git's subcommands. Because it has a deeply nested command API similar to this, and uses positional arguments with implied semantic meaning, it too usually guesses the appropriate partition. But it uses --
to disambiguate when there could be two potential interpretations. For example, if I have a file called HEAD
in my project it requires me to add a --
:
$ git checkout HEAD
# fatal: ambiguous argument 'HEAD': both revision and filename
# Use '--' to separate paths from revisions, like this:
# 'git <command> [<revision>...] -- [<file>...]'
But either of these work, because they're not ambiguous:
$ git checkout HEAD --
$ git checkout -- HEAD
from cri.
I find argument_groups
a bit awkward to work with. Here’s a sample implementation of checkout
I came up with:
class SampleGroupsRunner < Cri::CommandRunner
def run
if files.empty?
puts "Checking out all files (revision #{revision})"
else
files.each do |file|
puts "Checking out file #{file} (revision #{revision})"
end
end
end
private
def files
if argument_groups.size == 2
argument_groups[1]
else
arguments.select do |a|
File.file?(a)
end
end
end
def revision
if argument_groups.size == 2
potential_revisions = argument_groups[0]
else
potential_revisions = arguments.select do |a|
!File.file?(a)
end
end
if potential_revisions.size > 1
$stderr.puts "Expected 0 or 1 revisions, got #{potential_revisions.size}"
exit 1
end
potential_revisions[0] || 'HEAD'
end
end
Sample output:
$ be ruby samples/sample_groups.rb HEAD
Checking out all files (revision HEAD)
$ be ruby samples/sample_groups.rb Gemfile
Checking out file Gemfile (revision HEAD)
$ be ruby samples/sample_groups.rb HEAD Gemfile NEWS.md
Checking out file Gemfile (revision HEAD)
Checking out file NEWS.md (revision HEAD)
$ be ruby samples/sample_groups.rb 0f3aa691 Gemfile
Checking out file Gemfile (revision 0f3aa691)
It’s a lot of code, and it doesn’t even detect ambiguous names like HEAD
.
from cri.
Comparison between master and feature/argument-groups
from cri.
Updated sample to show how to detect ambiguous arguments.
from cri.
And here's Forematter updated to use argument_groups
from cri.
Nice! Are you satisfied with argument_groups
?
from cri.
Yep. Works for me!
from cri.
Related Issues (20)
- Feature: Arbitrary arguments HOT 3
- Is the argument: option required when using multiple: true HOT 3
- uninitialized constant Cri::Error HOT 2
- Options propagation on subcommand tree HOT 4
- iterating over arguments fails since 2.12 HOT 2
- colored breaks awesome_print HOT 3
- Release 2.15.4 breaks semver HOT 10
- Commands without `summary` does not even show command name HOT 2
- Forbidden optional parameters set to ` nil` when not present in command line HOT 15
- Version 2.15.7 causes runtime error HOT 1
- Options from one command appear in other commands
- Consider making it clearer that --verbose is an option for the help subcommand itself HOT 4
- 2.15.8 breaks nanoc HOT 6
- Handler for command line parameters and option errors
- Platform colour detection uses outdated technique on Windows HOT 3
- Warning with ruby 2.7 HOT 3
- [Feature Request]: Change symbols with dashes/hyphens to underscores
- Add negatable long-form options HOT 1
- Enhance #param to allow :optional and a description
- make a new release? HOT 2
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 cri.