Class: Balmora::Cli
- Inherits:
-
Object
- Object
- Balmora::Cli
- Defined in:
- lib/balmora/cli.rb
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#err ⇒ Object
Returns the value of attribute err.
-
#out ⇒ Object
Returns the value of attribute out.
-
#rescue ⇒ Object
Returns the value of attribute rescue.
Instance Method Summary collapse
- #_create_options ⇒ Object
- #help(state) ⇒ Object
-
#initialize ⇒ Cli
constructor
A new instance of Cli.
- #run(argv) ⇒ Object
- #task_help(statetask_name, task) ⇒ Object
Constructor Details
Instance Attribute Details
#err ⇒ Object
Returns the value of attribute err.
8 9 10 |
# File 'lib/balmora/cli.rb', line 8 def err @err end |
#out ⇒ Object
Returns the value of attribute out.
8 9 10 |
# File 'lib/balmora/cli.rb', line 8 def out @out end |
#rescue ⇒ Object
Returns the value of attribute rescue.
8 9 10 |
# File 'lib/balmora/cli.rb', line 8 def rescue @rescue end |
Instance Method Details
#_create_options ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/balmora/cli.rb', line 20 def () @options = { config: { shortcut: 'c', description: 'Configuration file; if not specified, balmora will ' + 'look configuration file at ~/.config/balmora/balmora.conf and ' + '/etc/balmora/balmora.conf in order', }, quite: { shortcut: 'q', flag: true, description: 'Quite mode; reports only errors', }, dry: { shortcut: 'd', flag: true, description: 'Dry run; will not run modify commands but report it to log', }, verbose: { shortcut: 'v', flag: true, description: 'Verbose mode; reports all executed commands', }, help: { shortcut: 'h', flag: true, description: 'Show help; if --config specified in arguments, shows ' + 'config help; if TASK specified, shows task help', }, } end |
#help(state) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/balmora/cli.rb', line 117 def help(state) tasks = state. config. get([:tasks], variables: false, default: []). collect() { |key, task| [' ' * 4 + key.to_s(), task[:description] || ''] } help = "Usage: balmora [common-options] task [task-options]\n\n" + "Options\n\n" + @arguments.help(@options) + "\n\n" + "Tasks:\n\n" + @arguments.format(tasks) return help end |
#run(argv) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/balmora/cli.rb', line 56 def run(argv) args, tail = @arguments.parse(@options, argv) state = Balmora::State.create(args, {}) if tail.nil?() if args[:help] == true @out.puts(help(state)) return 1 end @err.puts('No task specified; use --help to see help') return 1 end if tail[0].start_with?('-') raise Error.new("Unknown option #{tail.first()}; use --help to see " + "options") end task_name, *task_argv = tail task = state.config.get([:tasks, task_name.to_sym()], default: nil, variables: false) if task.nil?() raise Error.new("Unknown task #{task_name.to_s().inspect()}; use " + "--help to see task list") end if args[:help] == true @out.puts(task_help(state, task_name, task)) return 1 end if task.has_key?(:arguments) task_args, task_args_tail = @arguments.parse(task[:arguments], task_argv) if !task_args_tail.nil?() raise Error.new("Unknown task option #{task_args_tail.first(). inspect()}; use --help #{task_name} to see task options") end else task_args = {} end state.arguments.merge!(task_args) STDIN.reopen("/dev/tty") # make sure that input to tty will return state.balmora.run(task_name, state) rescue => error if !@rescue || true raise error end @err.puts('Error: ' + error.to_s()) if args[:debug] @err.puts(error.backtrace) end return 1 end |
#task_help(statetask_name, task) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/balmora/cli.rb', line 136 def task_help(statetask_name, task) help = "Usage: balmora [common-optinos] #{task_name} [task-options]\n\n" + "Description:\n\n" + ( task[:description] && @arguments.format([[' ', task[:description]]]) || ((' ' * 4) + 'No description provided') ) + "\n\n" + "Arguments:\n\n" + ( task[:arguments] && @arguments.help(statetask[:arguments]) || ((' ' * 4) + 'Task has no arguments') ) return help end |