Skip to content

Commit a236023

Browse files
authored
Merge pull request #38 from DannyBen/param
Add param command
2 parents ed71f60 + eafb7c9 commit a236023

File tree

10 files changed

+128
-32
lines changed

10 files changed

+128
-32
lines changed

cucumber.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
<%
2-
rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
3-
rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
4-
std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~@wip"
2+
std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict"
53
%>
64

75
default: --no-source <%= std_opts %> features
8-
wip: --tags @wip:3 --wip features
9-
current: -s <%= std_opts %> --tags @current features
10-
rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip
6+

examples/c_documented/Runfile

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#---
2-
# Global Commands
2+
# Global Definitions
33
# each Runfile may have a summary and a version. Both are optional.
44
#---
55

@@ -36,6 +36,18 @@ help "Say hello to <name>. Use --long to show a longer greeting, and this line i
3636
option "--long", "Show a longer greeting"
3737
option "-c --color", "Use colored output"
3838

39+
# the third optional parameter can be used to specify a different
40+
# (additional) title that will appear above all options
41+
option "--quiet", "Do not output anything", "Special Options"
42+
43+
# param adds a help text for a positional parameter under the Parameters
44+
# section.
45+
param "SOURCE", "Source file to copy"
46+
47+
# the third optional parameter can be used to specify a different title, in
48+
# a way similar to the 'option' command.
49+
param "SOURCE", "Source file to copy", "Copy Options"
50+
3951
# example adds an example command.
4052
# the command accepts one parameter: the example command.
4153
# you do not need to include "run" at the beginning of your example, it will
@@ -74,3 +86,7 @@ action :up do |args|
7486
say args['--high'] ? "\b..." : "and get down."
7587
end
7688

89+
# the optional command 'endcommand' closes any 'command' block that
90+
# was opened. it can be used to create several namespaces in the same
91+
# Runfile.
92+
endcommand

examples/u_params/Runfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# this example shows how to add positional params to the generated help text.
2+
# note that this is only used in the generated help, and has no other purpose.
3+
# as with docopt, you can use either the <param> or PARAM syntax.
4+
#
5+
# run 'run -h' to see how it works
6+
7+
usage "connect HOST PORT"
8+
param "HOST", "Host to connect to"
9+
param "PORT", "Port to connect to"
10+
action :connect do |args|
11+
say "Connecting #{args['HOST']}:#{args['PORT']}"
12+
end
13+
14+
# specifying a third parameter to the `param` command, will add it under a
15+
# different caption
16+
17+
usage "ssh USER PASS"
18+
param "USER", "SSH user", "SSH Parameters"
19+
param "PASS", "SSH password", "SSH Parameters"
20+
action :ssh do |args|
21+
say "Connecting as #{args['USER']}:#{args['PASS']}"
22+
end

examples/u_params/output.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Runfile
2+
3+
Usage:
4+
run connect HOST PORT
5+
run ssh USER PASS
6+
run (-h|--help)
7+
8+
Options:
9+
-h --help
10+
Show this screen
11+
12+
Parameters:
13+
HOST
14+
Host to connect to
15+
16+
PORT
17+
Port to connect to
18+
19+
SSH Parameters:
20+
USER
21+
SSH user
22+
23+
PASS
24+
SSH password

features/m_make_runfile.feature

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Feature: Make Runfile
66
Background:
77
Given the folder "sandbox" exists
88

9-
Scenario: "Show help when running without a Runfile"
9+
Scenario: Show help when running without a Runfile
1010
Given I am in the "sandbox" folder
1111
And the folder is empty
1212
When I run "run"
@@ -22,7 +22,7 @@ Scenario: Create a runfile
2222
And the file "Runfile" should exist
2323
And the file "Runfile" should contain "hello [<name> --color]"
2424

25-
Scenario: "Create a named runfile"
25+
Scenario: Create a named runfile
2626
Given I am in the "sandbox" folder
2727
And the folder is empty
2828
When I run "run new one"
@@ -33,15 +33,15 @@ Scenario: "Create a named runfile"
3333
And the output should match "Tip: Type 'run new'..."
3434
And the output should match "run .*sandbox"
3535

36-
Scenario: "Dont show the new tip if the user is already trained"
36+
Scenario: Dont show the new tip if the user is already trained
3737
Given I am in the "sandbox" folder
3838
And the file "one.runfile" exists
3939
And the file "two.runfile" exists
4040
And the file "three.runfile" exists
4141
When I run "run"
4242
Then the output should not match "Tip: Type 'run new' or 'run new name'..."
4343

44-
Scenario: "Dont show the new tip if .runfile is present"
44+
Scenario: Dont show the new tip if .runfile is present
4545
Given I am in the "examples/s_settings" folder
4646
When I run "run new"
4747
Then the output should match "Usage: run <file>"

features/n_exec.feature

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,52 @@ Background:
77
Given I am in the "examples/r_exec" folder
88
And the folder "tmp" exists
99

10-
Scenario: "Run an external command"
10+
Scenario: Run an external command
1111
When I run "run solve"
1212
Then the output should say "If there was a problem, yo, I'll solve it"
1313

14-
Scenario: "Execute a before and after block"
14+
Scenario: Execute a before and after block
1515
When I run "run solve"
1616
Then the output should be like "output.txt"
1717

18-
Scenario: "Execute a background job"
18+
Scenario: Execute a background job
1919
When I run "run behind"
2020
Then the output should say "> exec ls >/dev/null 2>&1"
2121

22-
Scenario: "Execute a background job with a given pid alias"
22+
Scenario: Execute a background job with a given pid alias
2323
When I run "run pid"
2424
Then the output should say "> exec ls >/dev/null 2>&1"
2525
And the file "ls.pid" should exist
2626
And the file "ls.pid" should match "\d+"
2727

28-
Scenario: "Execute a background job storing pid in another folder"
28+
Scenario: Execute a background job storing pid in another folder
2929
Given the file "tmp/pidfile.pid" does not exist
3030
When I run "run piddir"
3131
Then the file "tmp/pidfile.pid" should exist
3232
And the file "tmp/pidfile.pid" should match "\d+"
3333
And the file "pidfile.pid" should not exist
3434

35-
Scenario: "Execute a background job and direct output to a log"
35+
Scenario: Execute a background job and direct output to a log
3636
Given the file "log.log" does not exist
3737
When I run "run log"
3838
And I wait "1" second
3939
Then the file "log.log" should exist
4040
And the file "log.log" should match "logged"
4141

42-
Scenario: "Stop a background job"
42+
Scenario: Stop a background job
4343
When I run "run sleep"
4444
And I run "run sleep --stop"
4545
Then the file "sleeper.pid" should not exist
4646
And the output should match "kill -s TERM \d+"
4747

48-
Scenario: "Stop a non existing background job"
48+
Scenario: Stop a non existing background job
4949
When I run "run error"
5050
Then the output should say "PID file not found"
5151

52-
Scenario: "Configure with a block"
52+
Scenario: Configure with a block
5353
When I run "run block-config"
5454
Then the output should say "pid_dir = what-a-lovely-folder"
5555

56-
Scenario: "Run without showing the command"
56+
Scenario: Run without showing the command
5757
When I run "run quiet"
5858
Then the output should match "BEFORE echo quietly\nquietly\nAFTER echo quietly"

features/p_params.feature

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Feature: Params
2+
In order to provide a better help text
3+
Users can document params
4+
5+
Scenario: Show proper help
6+
Given I am in the "examples/u_params" folder
7+
When I run "run -h"
8+
Then the output should be like "output.txt"
9+
And the output should have "Parameters:"
10+
And the output should have "SSH Parameters:"
11+

lib/runfile/docopt_helper.rb

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def initialize(options)
2222
@summary = options.summary
2323
@actions = options.actions
2424
@options = options.options
25+
@params = options.params
2526
@examples = options.examples
2627
end
2728

@@ -35,6 +36,7 @@ def docopt
3536
doc += docopt_usage
3637
doc += docopt_commands width
3738
doc += docopt_options width
39+
doc += docopt_params width
3840
doc += docopt_examples width
3941
doc.join "\n"
4042
end
@@ -74,17 +76,12 @@ def docopt_options(width)
7476
@options['Options'] = {} unless @options['Options']
7577
@options['Options']['-h --help'] = 'Show this screen'
7678
@options['Options']['--version'] = 'Show version number' if @version
79+
section_block @options, width
80+
end
7781

78-
doc = []
79-
@options.each do |scope, values|
80-
doc << "#{scope}:"
81-
values.each do |flag, text|
82-
helpline = " #{text}"
83-
wrapped = word_wrap helpline, width
84-
doc << " #{flag}\n#{wrapped}\n"
85-
end
86-
end
87-
doc
82+
# Return all docopt params for 'Params' section
83+
def docopt_params(width)
84+
section_block @params, width
8885
end
8986

9087
# Return all docopt lines for the 'Examples' section
@@ -101,6 +98,21 @@ def docopt_examples(width)
10198
doc
10299
end
103100

101+
# Return a generic block containing scope section (e.g. "Options"),
102+
# followed by key value paragraphs.
103+
def section_block(definitions, width)
104+
doc = []
105+
definitions.each do |scope, values|
106+
doc << "#{scope}:"
107+
values.each do |label, text|
108+
helpline = " #{text}"
109+
wrapped = word_wrap helpline, width
110+
doc << " #{label}\n#{wrapped}\n"
111+
end
112+
end
113+
doc
114+
end
115+
104116
# Call the docopt handler, which will either return a parsed
105117
# arguments list, or halt execution and show usage.
106118
def args(argv)

lib/runfile/dsl.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ def option(flag, text, scope=nil)
4343
Runner.instance.add_option flag, text, scope
4444
end
4545

46+
# Add a parameter (can be called multiple times)
47+
# param 'FOLDER', 'Folder to copy'
48+
def param(name, text, scope=nil)
49+
Runner.instance.add_param name, text, scope
50+
end
51+
4652
# Set an example command (can be called multiple times)
4753
# example 'server --background'
4854
def example(text)

lib/runfile/runner.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class Runner
1313
include SettingsMixin
1414

1515
attr_accessor :last_usage, :last_help, :name, :version,
16-
:summary, :namespace, :superspace, :actions, :examples, :options
16+
:summary, :namespace, :superspace, :actions, :examples, :options,
17+
:params
1718

1819
# Initialize all variables to sensible defaults.
1920
def initialize
@@ -23,6 +24,7 @@ def initialize
2324
@namespace = nil # dsl: command
2425
@actions = {} # dsl: action
2526
@options = {} # dsl: option
27+
@params = {} # dsl: param
2628
@examples = [] # dsl: example
2729
@name = "Runfile" # dsl: name
2830
@version = false # dsl: version
@@ -72,6 +74,13 @@ def add_option(flag, text, scope=nil)
7274
@options[scope][flag] = text
7375
end
7476

77+
# Add a patameter and its help text.
78+
def add_param(name, text, scope=nil)
79+
scope or scope = 'Parameters'
80+
@params[scope] ||= {}
81+
@params[scope][name] = text
82+
end
83+
7584
# Add example command.
7685
def add_example(command)
7786
@examples << (@namespace ? "#{@namespace} #{command}" : command)

0 commit comments

Comments
 (0)