1 # coding: utf-8
2 # frozen_string_literal: true
3
4 require 'optparse'
5
6 require_relative '../version'
7
8
9 module Umu
10
11 module Commander
12
13 module_function
14
15 def parse_option(args, init_pref)
16 ASSERT.kind_of args, ::Array
17 ASSERT.kind_of init_pref, E::Preference
18
19 interactive_mode = false
20 trace_mode = false
21 dump_mode = false
22 no_prelude = false
23
24 mut_args = args.dup
25
26 OptionParser.new do |opts|
27 opts.on(
28 '-i', '--[no-]interactive',
29 'Interactive execution (REPL)'
30 ) do |answer|
31 interactive_mode = true
32 end
33
34 opts.on(
35 '-t', '--[no-]trace',
36 'Enable trace'
37 ) do |answer|
38 trace_mode = true
39 end
40
41 opts.on(
42 '-d', '--[no-]dump',
43 'Enable dump'
44 ) do |answer|
45 dump_mode = true
46 end
47
48 opts.on(
49 '--no-prelude',
50 'No loading standard prelude'
51 ) do |answer|
52 no_prelude = true
53 end
54
55 begin
56 opts.banner = 'umu [OPTION ..] SCRIPT_FILE ..'
57 opts.version = VERSION
58 opts.parse! mut_args
59 rescue OptionParser::ParseError => exception
60 raise X::CommandError.new exception.to_s
61 end
62 end
63
64 pref = init_pref
65 .update_interactive_mode(interactive_mode)
66 .update_trace_mode( trace_mode)
67 .update_dump_mode( dump_mode)
68 .update_no_prelude( no_prelude)
69
70 pair = [pref, mut_args]
71 ASSERT.tuple_of pair, [E::Preference, ::Array]
72 end
73
74 end # Umu::Commander
75
76 end # Umu