Class: Luban::CLI::Base
- Inherits:
-
Object
show all
- Includes:
- Commands
- Defined in:
- lib/luban/cli/base/dsl.rb,
lib/luban/cli/base/core.rb,
lib/luban/cli/base/parse.rb
Defined Under Namespace
Classes: InvalidCommand, MissingCommand, MissingRequiredArguments, MissingRequiredOptions
Constant Summary
collapse
- DefaultSummaryWidth =
32
- DefaultSummaryIndent =
4
- DefaultTitleIndent =
2
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#action(method_name = nil, &blk) ⇒ Object
-
#action!(method_name = nil, &blk) ⇒ Object
-
#argument(name, desc, **config, &blk) ⇒ Object
-
#default_prefix ⇒ Object
-
#default_program_name ⇒ Object
-
#desc(string) ⇒ Object
-
#help(short: :h, desc: "Show this help message.", &blk) ⇒ Object
(also: #auto_help)
-
#initialize(app, action_name, prefix: default_prefix, auto_help: true, &config_blk) ⇒ Base
constructor
-
#long_desc(string) ⇒ Object
-
#option(name, desc, nullable: false, **config, &blk) ⇒ Object
-
#parse(argv = default_argv) ⇒ Object
-
#parse!(argv = default_argv) ⇒ Object
-
#parser ⇒ Object
-
#program(name) ⇒ Object
-
#reset ⇒ Object
-
#show_help ⇒ Object
-
#show_help_for_command(args:, **params) ⇒ Object
-
#show_version ⇒ Object
-
#switch(name, desc, negatable: false, **config, &blk) ⇒ Object
Methods included from Commands
included
Constructor Details
#initialize(app, action_name, prefix: default_prefix, auto_help: true, &config_blk) ⇒ Base
Returns a new instance of Base.
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/luban/cli/base/core.rb', line 38
def initialize(app, action_name, prefix: default_prefix, auto_help: true, &config_blk)
@app = app
@action_name = action_name
@prefix = prefix
@action_method = "#{@prefix}#{@action_name}"
@action_defined = false
@program_name = default_program_name
@options = {}
@arguments = {}
@summary = ''
@description = ''
@version = ''
@default_argv = ARGV
@result = { cmd: nil, argv: @default_argv, args: {}, opts: {} }
@title_indent = DefaultTitleIndent
@summary_width = DefaultSummaryWidth
@summary_indent = DefaultSummaryIndent
configure(&config_blk)
setup_default_action unless @action_defined
self.auto_help if auto_help
end
|
Class Attribute Details
.config_blk ⇒ Object
Returns the value of attribute config_blk.
7
8
9
|
# File 'lib/luban/cli/base/dsl.rb', line 7
def config_blk
@config_blk
end
|
Instance Attribute Details
#action_method ⇒ Object
Returns the value of attribute action_method.
24
25
26
|
# File 'lib/luban/cli/base/core.rb', line 24
def action_method
@action_method
end
|
#app ⇒ Object
Returns the value of attribute app.
22
23
24
|
# File 'lib/luban/cli/base/core.rb', line 22
def app
@app
end
|
#arguments ⇒ Object
Returns the value of attribute arguments.
27
28
29
|
# File 'lib/luban/cli/base/core.rb', line 27
def arguments
@arguments
end
|
#default_argv ⇒ Object
Returns the value of attribute default_argv.
32
33
34
|
# File 'lib/luban/cli/base/core.rb', line 32
def default_argv
@default_argv
end
|
#description ⇒ Object
Returns the value of attribute description.
29
30
31
|
# File 'lib/luban/cli/base/core.rb', line 29
def description
@description
end
|
#options ⇒ Object
Returns the value of attribute options.
26
27
28
|
# File 'lib/luban/cli/base/core.rb', line 26
def options
@options
end
|
#prefix ⇒ Object
Returns the value of attribute prefix.
23
24
25
|
# File 'lib/luban/cli/base/core.rb', line 23
def prefix
@prefix
end
|
#program_name ⇒ Object
Returns the value of attribute program_name.
25
26
27
|
# File 'lib/luban/cli/base/core.rb', line 25
def program_name
@program_name
end
|
#result ⇒ Object
Returns the value of attribute result.
31
32
33
|
# File 'lib/luban/cli/base/core.rb', line 31
def result
@result
end
|
#summary ⇒ Object
Returns the value of attribute summary.
28
29
30
|
# File 'lib/luban/cli/base/core.rb', line 28
def summary
@summary
end
|
#summary_indent ⇒ Object
Returns the value of attribute summary_indent.
36
37
38
|
# File 'lib/luban/cli/base/core.rb', line 36
def summary_indent
@summary_indent
end
|
#summary_width ⇒ Object
Returns the value of attribute summary_width.
35
36
37
|
# File 'lib/luban/cli/base/core.rb', line 35
def summary_width
@summary_width
end
|
#title_indent ⇒ Object
Returns the value of attribute title_indent.
34
35
36
|
# File 'lib/luban/cli/base/core.rb', line 34
def title_indent
@title_indent
end
|
#version(ver = nil, short: :v, desc: "Show #{program_name} version.", &blk) ⇒ Object
Returns the value of attribute version.
73
74
75
76
77
78
79
80
|
# File 'lib/luban/cli/base/dsl.rb', line 73
def version(ver = nil, short: :v, desc: "Show #{program_name} version.", &blk)
if ver.nil?
@version
else
@version = ver.to_s
switch :version, desc, short: short, &blk
end
end
|
Class Method Details
9
|
# File 'lib/luban/cli/base/dsl.rb', line 9
def configure(&blk); @config_blk = blk; end
|
.help_command(**opts, &blk) ⇒ Object
Also known as:
auto_help_command
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/luban/cli/base/dsl.rb', line 11
def help_command(**opts, &blk)
if block_given?
command(**opts, &blk)
else
command(:help, **opts) do
desc "List all commands or help for one command"
argument :command, "Command to help for",
type: :symbol, required: false
action :show_help_for_command
end
end
end
|
Instance Method Details
#action(method_name = nil, &blk) ⇒ Object
84
85
86
|
# File 'lib/luban/cli/base/dsl.rb', line 84
def action(method_name = nil, &blk)
create_action(method_name, preserve_argv: true, &blk)
end
|
#action!(method_name = nil, &blk) ⇒ Object
88
89
90
|
# File 'lib/luban/cli/base/dsl.rb', line 88
def action!(method_name = nil, &blk)
create_action(method_name, preserve_argv: false, &blk)
end
|
#argument(name, desc, **config, &blk) ⇒ Object
54
55
56
|
# File 'lib/luban/cli/base/dsl.rb', line 54
def argument(name, desc, **config, &blk)
@arguments[name] = Argument.new(name, desc, **config, &blk)
end
|
#default_prefix ⇒ Object
63
|
# File 'lib/luban/cli/base/core.rb', line 63
def default_prefix; ''; end
|
#default_program_name ⇒ Object
69
70
71
|
# File 'lib/luban/cli/base/core.rb', line 69
def default_program_name
@default_program_name ||= File.basename($0, '.*')
end
|
#desc(string) ⇒ Object
30
31
32
|
# File 'lib/luban/cli/base/dsl.rb', line 30
def desc(string)
@summary = string.to_s unless string.nil?
end
|
#help(short: :h, desc: "Show this help message.", &blk) ⇒ Object
Also known as:
auto_help
58
59
60
|
# File 'lib/luban/cli/base/dsl.rb', line 58
def help(short: :h, desc: "Show this help message.", &blk)
switch :help, desc, short: short, &blk
end
|
#long_desc(string) ⇒ Object
34
35
36
|
# File 'lib/luban/cli/base/dsl.rb', line 34
def long_desc(string)
@description = string.to_s unless string.nil?
end
|
#option(name, desc, nullable: false, **config, &blk) ⇒ Object
38
39
40
41
42
43
44
|
# File 'lib/luban/cli/base/dsl.rb', line 38
def option(name, desc, nullable: false, **config, &blk)
@options[name] = if nullable
NullableOption.new(name, desc, **config, &blk)
else
Option.new(name, desc, **config, &blk)
end
end
|
#parse(argv = default_argv) ⇒ Object
4
5
6
7
|
# File 'lib/luban/cli/base/parse.rb', line 4
def parse(argv=default_argv)
argv = argv.dup
parse!(argv)
end
|
#parse!(argv = default_argv) ⇒ Object
9
10
11
12
13
14
15
16
|
# File 'lib/luban/cli/base/parse.rb', line 9
def parse!(argv=default_argv)
if commands.empty?
parse_without_commands(argv)
else
parse_with_commands(argv)
end
update_result(argv)
end
|
#parser ⇒ Object
65
66
67
|
# File 'lib/luban/cli/base/core.rb', line 65
def parser
@parser ||= create_parser
end
|
#program(name) ⇒ Object
26
27
28
|
# File 'lib/luban/cli/base/dsl.rb', line 26
def program(name)
@program_name = name.to_s unless name.nil?
end
|
#reset ⇒ Object
73
74
75
76
77
|
# File 'lib/luban/cli/base/core.rb', line 73
def reset
@options.each_value { |o| o.reset }
@arguments.each_value { |a| a.reset }
@result = { cmd: nil, argv: @default_argv, args: {}, opts: {} }
end
|
#show_help ⇒ Object
63
|
# File 'lib/luban/cli/base/dsl.rb', line 63
def show_help; puts parser.help; end
|
#show_help_for_command(args:, **params) ⇒ Object
65
66
67
68
69
70
71
|
# File 'lib/luban/cli/base/dsl.rb', line 65
def show_help_for_command(args:, **params)
if args[:command].nil?
show_help
else
commands[args[:command]].show_help
end
end
|
#show_version ⇒ Object
82
|
# File 'lib/luban/cli/base/dsl.rb', line 82
def show_version; puts parser.ver; end
|
#switch(name, desc, negatable: false, **config, &blk) ⇒ Object
46
47
48
49
50
51
52
|
# File 'lib/luban/cli/base/dsl.rb', line 46
def switch(name, desc, negatable: false, **config, &blk)
@options[name] = if negatable
NegatableSwitch.new(name, desc, **config, &blk)
else
Switch.new(name, desc, **config, &blk)
end
end
|