Module: IParty::CLI::Application::Options
- Included in:
- IParty::CLI::Application
- Defined in:
- lib/iparty/cli/application/options.rb
Instance Method Summary collapse
- #colorized_help_text ⇒ Object
- #default_options ⇒ Object
-
#init_optparse ⇒ Object
rubocop:disable Layout/SpaceInsideParens, Metrics/AbcSize – readability.
- #loadrc ⇒ Object
-
#parse_options! ⇒ Object
rubocop:enable Layout/SpaceInsideParens, Metrics/AbcSize.
- #require_resolv ⇒ Object
Instance Method Details
#colorized_help_text ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/iparty/cli/application/options.rb', line 103 def colorized_help_text @optparse.to_s.split("\n").map do |line| if line.start_with?("Usage:") words = line.split [ colorize(words[0]), colorize(words[1], :white), colorize(words[2], :yellow), colorize(words[3..].join(" "), :cyan), ].join(" ") elsif line.start_with?("#") colorize(line, :blue) elsif line.strip.start_with?("-") summary_width = @optparse.summary_indent.length + @optparse.summary_width optstr = line[...summary_width] optdesc = line[summary_width..] optdesc&.gsub!(/(\[default: [^\]]+\])/){ colorize(_1, :black) } [ colorize(optstr, :cyan), colorize(optdesc), ].join else colorize(line) end end end |
#default_options ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/iparty/cli/application/options.rb', line 7 def { debug: @argv.include?("--debug"), stdin: false, # --stdin colorize: true, # -m summarize: true, # -a resolve: false, # -r action: :info, # -d formatter: "pretty", # -f lang: "en", # -l only: [], # -o except: [], # -e # format latlong when summarizing (--no-a) fmt_latlong: "https://www.google.com/maps?q=%f,%f", # refresh stale mmdb files (:always, :missing, maxAge in seconds as Numeric) mmdb_fetch_when: 14 * 24 * 60 * 60, # 14.days } end |
#init_optparse ⇒ Object
rubocop:disable Layout/SpaceInsideParens, Metrics/AbcSize – readability
53 54 55 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 |
# File 'lib/iparty/cli/application/options.rb', line 53 def init_optparse OptionParser.new do |opts| opts.summary_width = 38 opts. = "Usage: iparty <IP|host...> [options]" expression_help = [ c("** matches .*", :cyan), c(" * matches [^.]*", :cyan), ] opts.separator("\n# Application options") opts.on("-a", "--[no-]all", "full non-summarized output") {|v| @opts[:summarize] = !v } opts.on("-f", "--format <FORMATTER>", String, "formatter (pretty|json|off) or template string [default: #{@opts[:formatter]}]") {|v| @opts[:formatter] = v } opts.on("-l", "--language <LANG>", String, "limit output to language (or all) [default: #{@opts[:lang]}]") {|v| @opts[:lang] = v } opts.on("-r", "--[no-]resolve", "resolve hosts and include hostnames in data (requires resolv)") {|v| @opts[:resolv] = v } opts.on("-o", "--only key,deep.key,*country*", Array, "list of key expressions (grep on full key)") {|v| @opts[:only] += v } opts.on("-e", "--except key,deep.key,sub*", Array, "list of key expressions (grep_v on full key)", *expression_help) {|v| @opts[:except] += v } opts.on( "--[no-]stdin", "read from stdin (space/line separated IPs or hosts)") {|v| @opts[:stdin] = v } opts.separator("\n# (Custom) actions") opts.on("-d", "--dispatch ACTION", String, "Dispatch given action, you may add your own") {|v| @opts[:action] = v.to_sym } opts.on( "--irb", "IRB repl with iparty context and helpers") { @opts[:action] = :irb } opts.separator("\n# MMDB actions") opts.on( "--mmdb-status", "Show mmdb file status") { exit(appinfo_mmdb_status ? 0 : 1) } opts.on( "--mmdb-fetch", "Fetch missing mmdb-editions") { ensure_mmdb_files! } opts.on( "--mmdb-update", "Update all mmdb-editions") { ensure_mmdb_files!(:always) } opts.separator("\n# General options") opts.on("-h", "--help", "Shows this help") { @opts[:action] = :help } opts.on("-v", "--version", "Shows version and mmdb info (and config with --debug)") { @opts[:action] = :appinfo } opts.on("-m", "--[no-]monochrome", "Don't or do colorize output") {|v| @opts[:colorize] = !v } opts.on( "--[no-]debug", "Enable debug, raise exceptions and print config with -v") {|v| @opts[:debug] = v } opts.on( "--no-rc", "Do not eval config.rb") end end |
#loadrc ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/iparty/cli/application/options.rb', line 28 def loadrc if @rc_disabled puts "[iparty-debug] skipping rc (disabled)" if @opts[:debug] return end unless @config_file.exist? && @config_file.readable? puts "[iparty-debug] skipping rc (inaccessible)" if @opts[:debug] return end puts "[iparty-debug] eval'ing rc #{@config_file}" if @opts[:debug] instance_eval @config_file.read(encoding: "utf-8"), @config_file.to_s end |
#parse_options! ⇒ Object
rubocop:enable Layout/SpaceInsideParens, Metrics/AbcSize
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/iparty/cli/application/options.rb', line 91 def return @opts if @options_parsed @options_parsed = true @optparse.parse!(@argv) require_resolv if @opts[:resolv] @opts rescue OptionParser::ParseError => ex puts colorized_help_text, nil @opts[:debug] ? raise(ex) : abort(c(ex., :red)) end |
#require_resolv ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/iparty/cli/application/options.rb', line 43 def require_resolv require "resolv" rescue LoadError warn c("This iparty feature requires the resolv gem to be installed.", :red) warn c("Resolution:", :yellow) warn c(" gem install resolv", :blue) exit 1 end |