Class: Slackware::Args

Inherits:
Object
  • Object
show all
Defined in:
lib/slackware/args.rb

Overview

Args is the unified arguement parser for the slack-utils utilities.

Class Method Summary collapse

Class Method Details

.parse(args, flags = nil, banner = nil) ⇒ Object



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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/slackware/args.rb', line 28

def self.parse(args,flags = nil, banner = nil)
  flags = [] unless flags.is_a?(Array)
  options = {}

  opts = OptionParser.new do |opts|
    if banner
      opts.banner = banner
    end
    if flags.include?(:color)
      opts.on("-c", "--color", "Colorize output") do |o|
        options[:color] = o
      end
    end
    if flags.include?(:epoch)
      opts.on("-e", "--epoch", "Print the time stamp in seconds since 1970-01-01 00:00:00 UTC ") do |o|
        options[:epoch] = o
      end
    end
    if flags.include?(:pkg_name)
      opts.on("-p", "--pkg [NAME]", "Package PKGNAME (loose match)") do |o|
        options[:pkg] = o
      end
    end
    if flags.include?(:pkg_version)
      opts.on("-V", "--Version [VERSION]", "Package VERSION (loose match)") do |o|
        options[:version] = o
      end
    end
    if flags.include?(:pkg_arch)
      opts.on("-a", "--arch [ARCH]", "Package ARCH (exact match)") do |o|
        options[:arch] = o
      end
    end
    if flags.include?(:pkg_build)
      opts.on("-b", "--build [BUILD]", "Package BUILD (exact match)") do |o|
        options[:build] = o
      end
    end
    if flags.include?(:pkg_tag)
      opts.on("-t", "--tag [TAG]", "Package TAG (loose match)") do |o|
        options[:tag] = o
      end
    end
    if flags.include?(:case_insensitive)
      opts.on("-i", "When searching, do a case insensitive match") do |o|
        options[:case_insensitive] = o
      end
    end
    if flags.include?(:force_all)
      opts.on("-f", "--force", "force me to show all files") do |o|
        options[:force] = o
      end
    end
    if flags.include?(:debug)
      opts.on("-D", "--debug", "show debugging output") do |o|
        options[:debug] = o
      end
    end

    opts.on("-v", "--version", "Display version of this software") do |o|
      printf("slack-utils version: %s, Slackware version: %s\n",
               Slackware::UTILS_VERSION,
               Slackware::System.version
            )
      exit(0)
    end
  end

  begin
    opts.parse!
    return options
  rescue OptionParser::InvalidOption => ex
    $stderr.write("ERROR: #{e.message}, see --help\n")
    exit(1)
  end
end