Class: Cli
- Inherits:
-
Object
- Object
- Cli
- Defined in:
- lib/jimuguri/cli.rb
Instance Method Summary collapse
- #actions ⇒ Object
-
#add_action(cmd, help, &blk) ⇒ Object
add action by given block.
- #add_option(short, long, message) ⇒ Object
- #description ⇒ Object
- #generate_help ⇒ Object
- #help ⇒ Object
- #help_string ⇒ Object
-
#init_help ⇒ Object
auto generate help.
-
#init_version ⇒ Object
auto generate version.
-
#initialize(name: 'sampleapp', description: 'sample app created by jimuguri', version: '0.0') ⇒ Cli
constructor
A new instance of Cli.
- #name ⇒ Object
- #options ⇒ Object
- #print_help ⇒ Object
- #print_version ⇒ Object
- #run(arg) ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(name: 'sampleapp', description: 'sample app created by jimuguri', version: '0.0') ⇒ Cli
Returns a new instance of Cli.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/jimuguri/cli.rb', line 5 def initialize(name: 'sampleapp', description: 'sample app created by jimuguri', version: '0.0') @name = name @description = description @version = version @cmds = {} @cmd_helps = {} init_help init_version @opt = OptionParser.new @options = {} @opt_helps = [] end |
Instance Method Details
#actions ⇒ Object
72 73 74 |
# File 'lib/jimuguri/cli.rb', line 72 def actions @cmds end |
#add_action(cmd, help, &blk) ⇒ Object
add action by given block
39 40 41 42 43 44 45 |
# File 'lib/jimuguri/cli.rb', line 39 def add_action(cmd, help, &blk) if !block_given? raise 'block must be given' end @cmds[cmd.to_sym] = blk @cmd_helps[cmd.to_sym] = help end |
#add_option(short, long, message) ⇒ Object
47 48 49 50 |
# File 'lib/jimuguri/cli.rb', line 47 def add_option(short, long, ) @opt.on("-#{short}", "--#{long}", ) {|v| @options[long.split(' ')[0].to_sym] = v} @opt_helps.push "-#{short} --#{long}\t#{}" end |
#description ⇒ Object
80 81 82 |
# File 'lib/jimuguri/cli.rb', line 80 def description @description end |
#generate_help ⇒ Object
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/jimuguri/cli.rb', line 106 def generate_help # concat commands commands = "COMMANDS:\n" @cmd_helps.map {|cmd, help| commands << " #{cmd.to_s}\t#{help}\n"} = "\nOPTIONS:\n" @opt_helps.map {|help| << " #{help}\n"} help_string + commands + end |
#help ⇒ Object
102 103 104 |
# File 'lib/jimuguri/cli.rb', line 102 def help generate_help end |
#help_string ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/jimuguri/cli.rb', line 117 def help_string <<~"HERE" NAME: #{@name} - #{@description} USAGE: #{@name} command [command options] [arguments...] VERSION: #{version} HERE end |
#init_help ⇒ Object
auto generate help
21 22 23 24 25 26 27 |
# File 'lib/jimuguri/cli.rb', line 21 def init_help help_proc = Proc.new do print_help end @cmds[:help] = help_proc @cmd_helps[:help] = 'Shows a list of commands or help for one command' end |
#init_version ⇒ Object
auto generate version
30 31 32 33 34 35 36 |
# File 'lib/jimuguri/cli.rb', line 30 def init_version version_proc = Proc.new do print_version end @cmds[:version] = version_proc @cmd_helps[:version] = 'Shows version' end |
#name ⇒ Object
76 77 78 |
# File 'lib/jimuguri/cli.rb', line 76 def name @name end |
#options ⇒ Object
88 89 90 |
# File 'lib/jimuguri/cli.rb', line 88 def @options end |
#print_help ⇒ Object
97 98 99 100 |
# File 'lib/jimuguri/cli.rb', line 97 def print_help puts help exit(0) end |
#print_version ⇒ Object
92 93 94 95 |
# File 'lib/jimuguri/cli.rb', line 92 def print_version puts version exit(0) end |
#run(arg) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/jimuguri/cli.rb', line 52 def run(arg) print_help if arg.size == 0 # disable optparse default help, version print_help if arg.include?('-h') || arg.include?('--help') print_version if arg.include?('-v') || arg.include?('--version') # parse begin @opt.parse!(arg) rescue print_help end action = @cmds[arg[0].to_sym] print_help if action.nil? action.call end |
#version ⇒ Object
84 85 86 |
# File 'lib/jimuguri/cli.rb', line 84 def version @version end |