admiral
Version, currently 1.1.323 versions
- 1.6.1latestApr 21, 2018
- 1.6.0not indexedNov 11, 2019
- 1.5.0not indexedNov 11, 2019
- 1.4.0not indexedNov 11, 2019
- 1.3.3not indexedNov 11, 2019
- 1.3.2not indexedNov 11, 2019
- 1.3.1not indexedNov 11, 2019
- 1.3.0not indexedNov 11, 2019
- 1.2.1not indexedNov 11, 2019
- 1.2.0not indexedNov 11, 2019
- 1.1.5not indexedNov 11, 2019
- 1.1.4not indexedNov 11, 2019
- 1.1.3not indexedNov 11, 2019
- 1.1.2not indexedNov 11, 2019
- 1.1.1not indexedNov 11, 2019
- 1.1.0not indexedNov 11, 2019
- 1.0.2not indexedNov 11, 2019
- 1.0.1not indexedNov 11, 2019
- 1.0.0-rc6not indexedNov 11, 2019
- 1.0.0-rc5not indexedNov 11, 2019
- 1.0.0-rc4not indexedNov 11, 2019
- 1.0.0-rc3not indexedNov 11, 2019
- 1.0.0-rc2not indexedNov 11, 2019
github.com/djmaze/admiral.cr
A robust command line DSL for command line interfaces written in Crystal.
Nothing has been indexed for 1.1.3 yet. The tag is recorded, its shard.yml has not been read, so the manifest and dependency list below are empty because they are unknown rather than because they are absent.
Installation
# Add this to your shard.yml
dependencies:
admiral:
github: djmaze/admiral.cr
version: ~> 1.1.3Then run:
shards installshard.yml
No shard.yml has been indexed for 1.1.3. You can read it on the repository.
Dependencies
Unknown: the shard.yml for this version has not been read yet.
README
This README is the one indexed from the repository at its latest ref, not from the tag for this version.
Admiral.cr
A robust DSL for writing command line interfaces written in Crystal.
Installation | Usage | Examples | Contributing | In the Wild
Installation
Add the following to your application's shard.yml file.
dependencies:
admiral:
github: jwaldrip/admiral.cr
Usage
Creating a new CLI | Flags | Arguments | Sub Commands | Command Help | Command Version
Creating a new CLI
You can define a CLI by creating a new class that inherits from Admiral::Command.
All your class needs to implement is a run method. Inside the run method will be
the logic of your cli application. The following is a very basic CLI. You can
run the command by invoking HelloWorld.run. By default this method will use
ARGV, but you can also pass Array(String) or String.
# hello_world.cr
require "admiral"
class HelloWorld < Admiral::Command
def run
puts "Hello World"
end
end
HelloWorld.run
$ crystal run ./hello_world.cr
Hello World
Flags
Flags can be added to the command. To define a flag use the define_flag macro.
Note: When defining flags, the underscore method name will translate to a hyphen on the command line. This can be overridden with the
long: my_nameoption when defining the flag.
Simple Flags
Simple flags are denoted only by a name and will compile to returning a String | Nil.
# hello_world.cr
class HelloWorld < Admiral::Command
define_flag planet
def run
puts "Hello #{flags.planet || "World"}"
end
end
HelloWorld.run
$ crystal build ./hello_world.cr
$ ./hello_world
Hello World
$ ./hello_world --planet Alderaan
Hello Alderaan
Typed Flags
Flags can also be assigned a type. This will result in a properly typed value when
calling flags.flag_name. By default flags are not required and will return a
Union including the type and Nil.
# hello_world.cr
class HelloWorld < Admiral::Command
define_flag number_of_greetings : UInt32, default: 1_u32, long: times
def run
flags.times.times do
puts "Hello World"
end
end
end
HelloWorld.run
$ crystal build ./hello_world.cr
$ ./hello_world --times 3
Hello World
Hello World
Hello World
Built in flag types
The following classes are assignable as flags by default:
StringBoolFloat32Float64Int8Int16Int32Int64UInt8UInt16UInt32UInt64
Pro Tip
To make anyClassorStructassignable as a flag, define a.new(value : ::Admiral::StringValue)or
#initialize(value : ::Admiral::StringValue).
Enumerable Flags
Enumerable flags allow for multiple values to be passed on the command line. For
example with a defined flag with Array(String) would return an array of String
values when calling the flag.
# hello_world.cr
class HelloWorld < Admiral::Command
define_flag citizens : Array(String), long: citizen
def run
flags.citizen.each do |citizen|
puts "Hello #{citizen}, citizen of Earth!"
end
end
end
HelloWorld.run
$ crystal build ./hello_world.cr
$ ./hello_world --citizen Jim --citizen Harry
Hello Jim, citizen of Earth!
Hello Harry, citizen of Earth!
Additional Flag Options
# hello_world.cr
class HelloWorld < Admiral::Command
define_flag number_of_greetings : UInt32,
description: "The number of times to greet the world",
default: 1_u32,
long: times,
short: t,
required: true
def run
flags.number_of_greetings.times do
puts "Hello World"
end
end
end
HelloWorld.run
| Option | Description |
|---|---|
description | The description of the flag to be used in auto generated help. |
default | The default value of the flag. |
long | The long version of the flag ex: long: times for --times. |
short | The short version of the flag ex: short: t for -t. |
required | Denotes if a flag is required. Required flags without a default value will raise an error when not specified at command invocation. |
Arguments
Arguments can be added to the command. To define a argument use the define_argument macro.
Simple Arguments
Simple arguments are denoted only by a name and will compile to returning a String | Nil.
# hello.cr
class Hello < Admiral::Command
define_argument planet
def run
puts "Hello #{arguments.planet || "World"}"
end
end
HelloWorld.run
$ crystal build ./world.cr
$ ./hello
Hello World
$ ./hello Alderaan
Hello Alderaan
Typed Arguments
Arguments can also be assigned a type. This will result in a properly typed value when
calling arguments.arg_name. By default arguments are not required and will return a
Union including the type and Nil.
# hello_world.cr
class HelloWorld < Admiral::Command
define_argument number_of_greetings : UInt32, default: 1_u32
def run
arguments.number_of_greetings.times do
puts "Hello World"
end
end
end
HelloWorld.run
$ crystal build ./hello_world.cr
$ ./hello_world 3
Hello World
Hello World
Hello World
Built in argument types
The following classes are assignable as arguments by default:
StringBoolFloat32Float64Int8Int16Int32Int64UInt8UInt16UInt32UInt64
Pro Tip
To make anyClassorStructassignable as a argument, define a.new(value : ::Admiral::StringValue)or
#initialize(value : ::Admiral::StringValue).
Additional Argument Options
# hello_world.cr
class HelloWorld < Admiral::Command
define_argument number_of_greetings : UInt32,
description: "The number of times to greet the world",
default: 1_u32,
required: true
def run
arguments.number_of_greetings.times do
puts "Hello World"
end
end
end
HelloWorld.run
| Option | Description |
|---|---|
description | The description of the argument to be used in auto generated help. |
default | The default value of the argument. |
required | Denotes if a argument is required. Required arguments without a default value will raise an error when not specified at command invocation. |
Note:
Required arguments cannot be defined after optional arguments.
Sub Commands
Sub commands can be added to the command. To define a sub command use the
register_sub_command macro. You also have the option to add a description for
the auto-generated help.
# hello.cr
class Hello < Admiral::Command
class Planetary < Admiral::Command
def run
puts "Hello World"
end
end
class Municipality < Admiral::Command
def run
puts "Hello Denver"
end
end
register_sub_command planet : Planetary
register_sub_command city : Municipality
def run
puts help
end
end
HelloWorld.run
$ crystal build ./hello.cr
$ ./hello planet
Hello World
$ ./hello city
Hello Denver
Command Help
Auto-generated Help
You can add a help command to your CLI by using the define_help macro.
define_help also takes a description argument to give additional information
about your command.
# hello.cr
class Hello < Admiral::Command
define_help description: "A command that says hello"
define_argument planet, default: "World"
def run
puts "Hello #{arguments.planet}"
end
end
$ crystal build ./hello.cr
$ ./hello --help
Usage:
./hello [flags...] <planet> [arg...]
A command that says hello
Flags:
--help
Arguments:
planet (default: World)
Custom Help
You can also generate your own custom help text.
# hello.cr
class Hello < Admiral::Command
define_help custom: "This is the help for my command"
def run
end
end
Command Version
Like most CLI applications, you can set a version flag.
# hello.cr
class Hello < Admiral::Command
define_version "1.0.0"
def run
end
end
$ crystal build ./hello.cr
$ hello --version
1.0.0
Examples
Example CLIs can be found in ./examples
In the wild
Here are some tools using Admiral.cr in the wild. Have your own you would like to plug? Submit a pull request!
Todo
- [x] Basic Flags
- [x] Typed Flags
- [x] Boolean Flags
- [x] Enum Flags
- [x] Named Arguments
- [x] Positional Arguments
- [x] Sub Commands
- [x] Documentation
- [ ] Fully Tested
- [ ] Bash Completion
- [ ] Zsh Completion
Contributing
See CONTRIBUTING for details on how to contribute.
Documentation
Built from the current release. The first visit to a release nobody has asked for starts its build.
Links
This release
- Version
1.1.3- Tagged
- Nov 11, 2019
- Commit
cb1275bcdca3- Indexed
- not yet
Dependents
Repository
github.com/djmaze/admiral.cr
Metadata
- Created
- Aug 19, 2026
- Updated
- Nov 12, 2026
- Synced
- Nov 12, 2026
- Versions
- 23