Method: Oughtve.parse_from

Defined in:
lib/oughtve/options.rb

.parse_from(arguments) ⇒ Object

Parse allowed options from given arguments



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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/oughtve/options.rb', line 55

def self.parse_from(arguments)
  result = OpenStruct.new

  # Grab time here to be a bit closer to true
  result.time = Time.now

  def result.action!(given)
    raise ArgumentError, "Two actions given: 1) --#{action}, 2) --#{given}" if action
    self.action = given
  end

  OptionParser.new do |opts|

    opts.banner = "Usage: #{$0} [--ACTION [OPTIONS]]"

    opts.separator ""
    opts.separator "  Actions:"


    # Actions

    opts.on "-s", "--scribe [TEXT]", "Enter a new note." do |text|
      result.action! :scribe
      result.text = text
    end

    opts.on "-S", "--strike [ID_OR_REGEXP]", "Strike out a note." do |id_or_regexp|
      result.action! :strike

      if id_or_regexp
        begin
          result.serial = Integer(id_or_regexp)
        rescue ArgumentError
          result.regexp = /#{Regexp.escape id_or_regexp}/
        end
      end
    end

    opts.on "-w", "--show [all | old]", "Show notes for tangent(s). [All notes / old chapters too]" do |specifier|
      result.action! :show

      if specifier
        case specifier
        when "all"
          result.all = true
        when "old"
          result.all = true
          result.old = true
        else
          raise ArgumentError, "--show #{specifier} is not a valid option!" 
        end
      end
    end

    opts.on "-l", "--list", "Show defined Tangents and their base directories." do
      result.action! :list
    end

    opts.on "-c", "--chapter ENDNOTE", "Mark end of chapter and start new one." do |endnote|
      result.action! :chapter
      result.endnote = endnote
    end


    # Options
    opts.separator ""
    opts.separator "  Maintenance Actions:"

    opts.on "-b", "--bootstrap", "Set up database and initial structures." do
      result.action! :bootstrap
    end

    opts.on "-n", "--new", "Create new Tangent. (Use -t to give it a name.)" do
      result.action! :tangent
    end

    opts.on "-D", "--delete [NAME]", "Delete tangent." do |name|
      result.action! :delete
      result.name = name
    end


    # Options
    opts.separator ""
    opts.separator "  Options:"

    opts.on "-d", "--directory DIR", "Use given directory instead of Dir.pwd." do |dir|
      result.dir = dir
    end

    opts.on "-i", "--id ID", "Use specific note ID." do |id|
      result.serial = id
    end

    opts.on "-m", "--match REGEXP", "Match note using regexp, specific branch only." do |regexp|
      result.regexp = /#{Regexp.escape regexp}/
    end

    opts.on "-t", "--tangent NAME", "Use named Tangent specifically." do |name|
      result.name = name
    end

    opts.on "-x", "--text TEXT", "Text to use for note." do |text|
      result.text = text
    end

    opts.on "-v", "--verbose", "Give extra information for some actions." do
      result.verbose = true
    end

    opts.on "-J", "--json", "JSON output" do
      result.format = :json
    end

    opts.on "-Y", "--yaml", "YAML output" do
      result.format = :yaml
    end


    # Bookkeeping stuff

    opts.on_tail "-h", "--help", "Display this message." do
      puts opts
      exit!
    end

  end.parse! arguments

rescue OptionParser::InvalidOption => e
  $stderr.print "\n#{e.message}\n\n"
  parse_from %w[ -h ]

else
  result.rest = arguments
  result.action = :scribe unless result.action
  result
end