mosop / cli Goto Github PK
View Code? Open in Web Editor NEWYet another Crystal library for building command-line interface applications.
License: MIT License
Yet another Crystal library for building command-line interface applications.
License: MIT License
As BlaXpirit was kind enough to point out on Crystal's IRC/Gitter, in order for the actual program to exit with the correct status code, the Cli should be called as exit(MyCommand.run)
.
Hi, i'm trying to copy the behavior of Javascript package managers in processing arguments e.g:
command install package1 package2
i tried to do it but what it outputs in the terminal is only the first item (because they are separated by a space not by a comma)
how can i achieve this?
Hi,
After reading the docs and the source, I couldn't find an easy way to check if an option is set or not.
For example, if I have this option:
string "-t", required: false
If I run without the "-t" flag but I want to check in my command if the flag is set, I can't do simply:
if options.t
I get a:
Missing hash key: "-t" (KeyError)
The only way I find is:
if options.__options__string.includes? "-t"
which is tedious to write.
Is there any other simpler solution for this?
I like to implement:
command --option subcommand --suboption
and the subcommand command can get the --option option.
E.g. if I'd like the following:
$ command sub-command --option1=foo sub-sub-command arg1 arg2
I'm looking at the example from optarg
here https://github.com/mosop/optarg#custom-validation
Could you provide an example of this in cli? The best I could do was (example code):
class Exec < Cli::Command
class Options
string "--id", required: true
Parser.on_validate do |parser|
if parser.args.strings["--id"].to_i?.nil?
parser.invalidate! "#{parser.args.strings["--id"]} is not an int"
end
end
end
end
I feel parser.args.strings["--id"]
is a bit clunky. It's matching on the exact string flag, with double -- in front, and not the nice options
we get inside run
. I'd love an example of validation inside the context of Cli.
In all examples the code is always executed programatically using #run
method. Example:
MyClass.run %w(hello)
This is useful for examples and testing, but in real world I would like to do:
./binary hello
I couldn't find any example or documentation explaining how can I "initialize" my CLI in order to my binary run it instead of calling it from inside my code with #run
methods.
How can I achieve that?
Since crystal-lang v0.24.0 IO is an abstract class, not a module
crystal-lang/crystal#4901
in lib/cli/src/lib/ios/pipe.cr:10: IO is not a module, it's a class
include IO
^~
With a structure like this:
include Cli
class To < SuperCommand
command "go-to", default: true
class GoTo < Command
def run
# ...
end
class Options
arg "where", required: true
end
end
class OtherCommand < Command
def run
# ...
end
end
class Help
header "Goes to specified location"
end
class Options
help
end
end
Now if i were to run to home
โ instead of running "go-to" with the argument "home" as "where"; it throws the help screen screen at me. Running to
alone says i need to specify the where argument.
What about making it so if a default command is set and has (required?) arguments, it checks wether whatever is passed after supercommand invoke is a subcommand or not? If it is not, pass it as an argument to the default command.
It shows the error with the following namespaces :
The error with the following code:
require "cli"
module Test::Test
class Main < Cli::Supercommand
class Help
header "Main header"
footer "Main footer"
end
end
end
The error here : https://gist.github.com/TechMagister/0e941cddeb31cd9cfdd63d714c633255
The error is:
Error: abstract `def Enumerable(T)#each(&block)` must be implemented by Callback::ResultSet(T)
The code is one of your samples:
require "cli"
class Hello < Cli::Command
class Options
bool "--bye"
arg "to"
end
def run
if args.bye?
print "Goodbye"
else
print "Hello"
end
puts " #{args.to}!"
end
end
Hello.run %w(world) # prints "Hello, world!"
Hello.run %w(--bye world) # prints "Goodbye, world!"
Hello, I'm getting an error with the exemple of subcommands from the readme
(https://github.com/mosop/cli#subcommands)
With the simple code (simplified) :
require "cli"
class Cake < Cli::Supercommand
command "chocolat"
class Options
help
end
module Commands
class Chocolat < Cli::Command
caption "winter only" #error here
end
end
end
I get the error :
Error in ./src/cli_test.cr:12: undefined method 'caption'
caption "winter only"
^~~~~~~
Thanks
Hi,
Is there a way to achieve someting like this :
cmd subcommand ARG [field[:type][:index] field[:type][:index]] [options]
Here is what I have :
arg "arg", required: true
Is there a way to do something like an array of infinite (or not) length with sub arguments : [field[:type][:index] field[:type][:index]]
?
My goal is to have this :
cmd subcommand Person age:integer:1 name role:string
cli 0.6.10 (Crystal 0.23.1)
I'm trying to organize my code by splitting the commands into its own file (in a one module dedicated to commands) and the root app.cr
file where the init
would take place. But when I try to import the commands like this:
require "cli"
require "./myapp/*"
module MyApp
class Myapp < Cli::Supercommand
version VERSION
class Options
help
version
end
include MyApp::Commands
end
Myapp.run ARGV
end
It doesn't work. I can't see any imported command popped up in the help section. Admiral (another cli lib) works just fine doing like this and I want to have a clean modular codebase so badly. Any Ideas? Thanks.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.