Skip to content

Commit

Permalink
Allow specifying descriptive argument for flag in short and/or long.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrrooijen committed Nov 12, 2019
1 parent cbcee29 commit 9fe4131
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 11 deletions.
60 changes: 60 additions & 0 deletions spec/flag_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require "./spec_helper"

describe Commander::Flag do

it "parses descriptive argument from short" do
flag = Commander::Flag.new do |flag|
flag.name = "suffix"
flag.description = flag.name
flag.short = "-s DIR"
flag.long = "--suffix"
flag.default = ""
end

flag.long.should eq("--suffix")
flag.short.should eq("-s")
flag.arg.should eq("DIR")
end

it "parses descriptive argument from long" do
flag = Commander::Flag.new do |flag|
flag.name = "suffix"
flag.description = flag.name
flag.short = "-s"
flag.long = "--suffix DIR"
flag.default = ""
end

flag.long.should eq("--suffix")
flag.short.should eq("-s")
flag.arg.should eq("DIR")
end

it "parses descriptive argument from short if both present" do
flag = Commander::Flag.new do |flag|
flag.name = "suffix"
flag.description = flag.name
flag.short = "-s DIR"
flag.long = "--suffix DIR"
flag.default = ""
end

flag.long.should eq("--suffix")
flag.short.should eq("-s")
flag.arg.should eq("DIR")
end

it "ignores descriptive argument if absent from both short and long" do
flag = Commander::Flag.new do |flag|
flag.name = "suffix"
flag.description = flag.name
flag.short = "-s"
flag.long = "--suffix"
flag.default = ""
end

flag.long.should eq("--suffix")
flag.short.should eq("-s")
flag.arg.should be_nil
end
end
52 changes: 43 additions & 9 deletions spec/help_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,41 @@ describe Commander do
cmd.long = "my program's (long) description."

cmd.flags.add do |flag|
flag.name = "suffix"
flag.short = "-s"
flag.name = "suffix-short"
flag.short = "-s DIR"
flag.default = ""
flag.description = "Suffix specified using a short flag and descriptive argument."
end

cmd.flags.add do |flag|
flag.name = "suffix-long"
flag.long = "--suffix DIR"
flag.default = ""
flag.description = "Suffix specified using a long flag and descriptive argument."
end

cmd.flags.add do |flag|
flag.name = "suffix-dir-on-short"
flag.short = "-s DIR"
flag.long = "--suffix"
flag.default = ""
flag.description = "Suffix of something."
flag.description = "Suffix specified using a short flag with descriptive argument and long flag."
end

cmd.flags.add do |flag|
flag.name = "suffix-dir-on-long"
flag.short = "-s"
flag.long = "--suffix DIR"
flag.default = ""
flag.description = "Suffix specified using a long flag with descriptive argument and short flag."
end

cmd.flags.add do |flag|
flag.name = "suffix-dir-on-both"
flag.short = "-s DIR"
flag.long = "--suffix DIR"
flag.default = ""
flag.description = "Suffix specified using a long flag with descriptive argument and short flag with descriptive argument."
end

cmd.flags.add do |flag|
Expand Down Expand Up @@ -68,12 +98,16 @@ describe Commander do
kill <pid> Kills server by pid.
Flags:
-e, --env The environment to run in. default: 'development'
-h, --help Help for this command.
-p, --port The port to bind to. default: 8080
-s, --suffix Suffix of something.
-t, --timeout The wait time before dropping the connection. default: 29.5
-v, --verbose Enable more verbose logging.
-e, --env The environment to run in. default: 'development'
-h, --help Help for this command.
-p, --port The port to bind to. default: 8080
-s, --suffix DIR Suffix specified using a long flag with descriptive argument and short flag with descriptive argument.
-s, --suffix DIR Suffix specified using a long flag with descriptive argument and short flag.
-s, --suffix DIR Suffix specified using a short flag with descriptive argument and long flag.
--suffix DIR Suffix specified using a long flag and descriptive argument.
-s DIR Suffix specified using a short flag and descriptive argument.
-t, --timeout The wait time before dropping the connection. default: 29.5
-v, --verbose Enable more verbose logging.
EOS

cli.commands.to_a[1].help.should eq <<-EOS
Expand Down
2 changes: 2 additions & 0 deletions src/commander/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ class Commander::Command
result += " #{flag.long}"
end

result += " #{flag.arg}" if flag.arg

description =
case flag.default
when Bool, ""
Expand Down
26 changes: 24 additions & 2 deletions src/commander/flag.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ class Commander::Flag
LONG_PATTERN = /^\-\-[a-zA-Z0-9-]+$/

property name : String
property short : String
property long : String
property default : Types
setter short : String
setter long : String
property description : String
property persistent : Bool

def initialize
@name = ""
@short = ""
@long = ""
@arg = ""
@description = ""
@default = nil
@persistent = false
Expand All @@ -27,6 +28,27 @@ class Commander::Flag
yield self
end

def short : String
@short.split[0]? || ""
end

def long : String
@long.split[0]? || ""
end

def arg : String | Nil
long_segments = @long.split[1..-1]? || Array(String).new
short_segments = @short.split[1..-1]? || Array(String).new

if long_segments.size > 0
long_segments.join(" ")
elsif short_segments.size > 0
short_segments.join(" ")
else
nil
end
end

protected def type
default.class
end
Expand Down

0 comments on commit 9fe4131

Please sign in to comment.