Class: Razor::CLI::Parse
- Inherits:
-
Object
- Object
- Razor::CLI::Parse
- Defined in:
- lib/razor/cli/parse.rb
Constant Summary collapse
- DEFAULT_RAZOR_API =
"http://localhost:8080/api"
Instance Attribute Summary collapse
-
#api_url ⇒ Object
readonly
Returns the value of attribute api_url.
Instance Method Summary collapse
- #dump_response? ⇒ Boolean
- #get_optparse ⇒ Object
- #help ⇒ Object
-
#initialize(args) ⇒ Parse
constructor
A new instance of Parse.
- #list_things(name, items) ⇒ Object
- #navigate ⇒ Object
- #show_help? ⇒ Boolean
Constructor Details
#initialize(args) ⇒ Parse
Returns a new instance of Parse.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/razor/cli/parse.rb', line 61 def initialize(args) parse_and_set_api_url(ENV["RAZOR_API"] || DEFAULT_RAZOR_API, :env) @args = args.dup rest = get_optparse.order(args) if rest.any? @navigation = rest else @option_help = true end end |
Instance Attribute Details
#api_url ⇒ Object (readonly)
Returns the value of attribute api_url.
59 60 61 |
# File 'lib/razor/cli/parse.rb', line 59 def api_url @api_url end |
Instance Method Details
#dump_response? ⇒ Boolean
55 56 57 |
# File 'lib/razor/cli/parse.rb', line 55 def dump_response? !!@dump end |
#get_optparse ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/razor/cli/parse.rb', line 9 def get_optparse @optparse ||= OptionParser.new do |opts| opts. = "Usage: razor [FLAGS] NAVIGATION\n" " or: razor shell" opts.on "-d", "--dump", "Dumps API output to the screen" do @dump = true end opts.on "-U", "--url URL", "The full Razor API URL, can also be set\n" + " "*37 + "with the RAZOR_API environment variable\n" + " "*37 + "(default #{DEFAULT_RAZOR_API})" do |url| parse_and_set_api_url(url, :opts) end opts.on "-h", "--help", "Show this screen" do @option_help = true end end end |
#help ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/razor/cli/parse.rb', line 39 def help output = get_optparse.to_s begin output << list_things("collections", navigate.collections) output << list_things("commands", navigate.commands) rescue output << "\nCould not connect to the server at #{@api_url}. More help is available after " output << "pointing\nthe client to a Razor server" end output end |
#list_things(name, items) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/razor/cli/parse.rb', line 32 def list_things(name, items) "\n #{name}:\n" + items.map {|x| x["name"]}.compact.sort.map do |name| " #{name}" end.join("\n") end |
#navigate ⇒ Object
72 73 74 |
# File 'lib/razor/cli/parse.rb', line 72 def navigate @navigate ||=Navigate.new(self, @navigation) end |
#show_help? ⇒ Boolean
51 52 53 |
# File 'lib/razor/cli/parse.rb', line 51 def show_help? !!@option_help end |