Skip to content

Commit

Permalink
Show "--help" in the help
Browse files Browse the repository at this point in the history
  • Loading branch information
erdnaxeli committed Apr 5, 2021
1 parent 68afcaf commit 08f3a1c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 16 deletions.
73 changes: 64 additions & 9 deletions spec/clip/help_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,20 @@ describe "Clip::Help" do
describe ".help" do
it "default to PROGRAM_NAME" do
EmptyHelp.help.should eq(
"Usage: #{PROGRAM_NAME}
"Usage: #{PROGRAM_NAME} [OPTIONS]
Options:
--help Show this message and exit.
"
)
end

it "show nothing for a empty command" do
EmptyHelp.help("empty").should eq(
"Usage: empty
"Usage: empty [OPTIONS]
Options:
--help Show this message and exit.
"
)
end
Expand All @@ -280,19 +286,23 @@ A simple command with one option.
Options:
--value TEXT [default: somevalue]
--help Show this message and exit.
"
)
end

it "handles long command doc" do
LongDocHelp.help("bin").should eq(
"Usage: bin VALUE
"Usage: bin [OPTIONS] VALUE
A simple command with one text argument. I am not sure what it does though. You
need to find out.
Arguments:
VALUE [required]
Options:
--help Show this message and exit.
"
)
end
Expand All @@ -303,6 +313,7 @@ Arguments:
Options:
--flag / --no-flag
--help Show this message and exit.
"
)
end
Expand All @@ -313,6 +324,7 @@ Options:
Options:
--value TEXT
--help Show this message and exit.
"
)
end
Expand All @@ -323,6 +335,7 @@ Options:
Options:
--value INTEGER
--help Show this message and exit.
"
)
end
Expand All @@ -333,6 +346,7 @@ Options:
Options:
--value FLOAT
--help Show this message and exit.
"
)
end
Expand All @@ -343,6 +357,7 @@ Options:
Options:
--value TEXT
--help Show this message and exit.
"
)
end
Expand All @@ -353,6 +368,7 @@ Options:
Options:
--value INTEGER
--help Show this message and exit.
"
)
end
Expand All @@ -363,6 +379,7 @@ Options:
Options:
--value FLOAT
--help Show this message and exit.
"
)
end
Expand All @@ -373,6 +390,7 @@ Options:
Options:
--flag / --no-flag [default: true]
--help Show this message and exit.
"
)
end
Expand All @@ -383,6 +401,7 @@ Options:
Options:
--value TEXT [default: somevalue]
--help Show this message and exit.
"
)
end
Expand All @@ -393,6 +412,7 @@ Options:
Options:
--value INTEGER [default: 42]
--help Show this message and exit.
"
)
end
Expand All @@ -403,6 +423,7 @@ Options:
Options:
--value FLOAT [default: 4.2]
--help Show this message and exit.
"
)
end
Expand All @@ -413,6 +434,7 @@ Options:
Options:
--value TEXT [default: [somevalue]]
--help Show this message and exit.
"
)
end
Expand All @@ -423,6 +445,7 @@ Options:
Options:
--value INTEGER [default: [42]]
--help Show this message and exit.
"
)
end
Expand All @@ -433,6 +456,7 @@ Options:
Options:
--value FLOAT [default: [4.2]]
--help Show this message and exit.
"
)
end
Expand All @@ -443,6 +467,7 @@ Options:
Options:
--flag / --no-flag [required]
--help Show this message and exit.
"
)
end
Expand All @@ -453,6 +478,7 @@ Options:
Options:
--flag [default: true]
--help Show this message and exit.
"
)
end
Expand All @@ -463,6 +489,7 @@ Options:
Options:
--value TEXT [required]
--help Show this message and exit.
"
)
end
Expand All @@ -473,6 +500,7 @@ Options:
Options:
--value INTEGER [required]
--help Show this message and exit.
"
)
end
Expand All @@ -483,6 +511,7 @@ Options:
Options:
--value FLOAT [required]
--help Show this message and exit.
"
)
end
Expand All @@ -493,6 +522,7 @@ Options:
Options:
--value TEXT [required]
--help Show this message and exit.
"
)
end
Expand All @@ -503,6 +533,7 @@ Options:
Options:
--value INTEGER [required]
--help Show this message and exit.
"
)
end
Expand All @@ -513,36 +544,46 @@ Options:
Options:
--value FLOAT [required]
--help Show this message and exit.
"
)
end

it "handles an argument" do
ArgumentHelp.help("bin").should eq(
"Usage: bin [VALUE]
"Usage: bin [OPTIONS] [VALUE]
Arguments:
VALUE
Options:
--help Show this message and exit.
"
)
end

it "handles an argument with a default value" do
DefaultArgumentHelp.help("bin").should eq(
"Usage: bin [VALUE]
"Usage: bin [OPTIONS] [VALUE]
Arguments:
VALUE [default: somevalue]
Options:
--help Show this message and exit.
"
)
end

it "handles a required argument" do
RequiredArgumentHelp.help("bin").should eq(
"Usage: bin VALUE
"Usage: bin [OPTIONS] VALUE
Arguments:
VALUE [required]
Options:
--help Show this message and exit.
"
)
end
Expand All @@ -553,6 +594,7 @@ Arguments:
Options:
--value TEXT This is a value. [default: somevalue]
--help Show this message and exit.
"
)
end
Expand All @@ -564,39 +606,49 @@ Options:
Options:
--value TEXT This is a value. [default: somevalue]
--number INTEGER And this is a number. [default: 4]
--help Show this message and exit.
"
)
end

it "handles argument doc" do
OneArgumentDoc.help("bin").should eq(
"Usage: bin VALUE
"Usage: bin [OPTIONS] VALUE
Arguments:
VALUE This is a value. [required]
Options:
--help Show this message and exit.
"
)
end

it "align argument doc" do
TwoArgumentsDoc.help("bin").should eq(
"Usage: bin NUMBER VALUE
"Usage: bin [OPTIONS] NUMBER VALUE
Arguments:
NUMBER And this is a number. [required]
VALUE This is a value. [required]
Options:
--help Show this message and exit.
"
)
end

it "follow argument index and doc" do
ArgumentsDocWithIndex.help("bin").should eq(
"Usage: bin NUMBER VALUE NAME
"Usage: bin [OPTIONS] NUMBER VALUE NAME
Arguments:
NUMBER And this is a number. [required]
VALUE This is a value. [required]
NAME [required]
Options:
--help Show this message and exit.
"
)
end
Expand All @@ -610,6 +662,7 @@ Arguments:
Options:
--number INTEGER And this is a number. [default: 4]
--help Show this message and exit.
"
)
end
Expand All @@ -621,6 +674,7 @@ Options:
Options:
--value TEXT ThisIsAVeryLongWord,TheGoalIsToMatchExactly80CharsAlmostYesDone!
and another line. [required]
--help Show this message and exit.
"
)
end
Expand All @@ -639,6 +693,7 @@ Options:
very very very very very very long doc for an argument.
[default: 4]
--booze / --no-booze And this the way we crush the party [default: true]
--help Show this message and exit.
"
)
end
Expand Down
18 changes: 11 additions & 7 deletions src/clip/help.cr
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ module Clip::Help
end
%}

{% help = "" %}

{% if options.size > 0 %}
{% help += " [OPTIONS]" %}
{% end %}
{% help = " [OPTIONS]" %}

{% for argument in arguments %}
{% help += " " %}
Expand Down Expand Up @@ -190,10 +186,10 @@ module Clip::Help
{% end %}
{% end %}

{% help += "\nOptions:\n" %}
{% if options.size > 0 %}
{% help += "\nOptions:\n" %}

{% max_option_size = 0 %}
{% max_option_size = "--help".size %}
{% for ivar in options %}
{% if ivar.annotation(Option) && ivar.annotation(Option).args.size > 0 %}
{% names = ivar.annotation(Option).args %}
Expand Down Expand Up @@ -330,7 +326,15 @@ module Clip::Help
{% help += current_option %}
{% help += "\n" %}
{% end %}
{% else %}
{% shift = 10 %}
{% end %}

{% help += " --help" %}
{% for i in 8...shift %}
{% help += ' ' %}
{% end %}
{% help += "Show this message and exit.\n" %}

{% if help != "" %}
"Usage: #{name}{{help.id}}"
Expand Down

0 comments on commit 08f3a1c

Please sign in to comment.