dim-toolkit 2.1.1 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/dim/options.rb CHANGED
@@ -1,116 +1,116 @@
1
- require 'optparse'
2
-
3
- require_relative 'globals'
4
- require_relative 'exit_helper'
5
-
6
- module Dim
7
- class Options
8
- MAX_COUNT = 10
9
-
10
- def self.reset
11
- OPTIONS[:filter] = ''
12
- OPTIONS[:title] = 'Requirements - Draft'
13
- OPTIONS[:folder] = nil
14
- OPTIONS[:input] = nil
15
- OPTIONS[:attributes] = nil
16
- OPTIONS[:config] = nil
17
- OPTIONS[:allow_missing] = false
18
- OPTIONS[:output_format] = 'in-place'
19
- OPTIONS[:type] = nil
20
- OPTIONS[:no_check_enclosed] = false
21
- OPTIONS[:silent] = nil
22
- end
23
- reset
24
-
25
- def self.parse(args = ARGV)
26
- op = OptionParser.new do |opts|
27
- opts.banner = "Usage: dim.rb <#{SUBCOMMANDS.keys.join('|')}> [options]"
28
- opts.separator "\nGeneral options:"
29
- opts.on('-h', '--help', 'prints this help') do
30
- Dim::ExitHelper.exit(code: 0, msg: opts)
31
- end
32
- opts.on('-v', '--version', 'prints version') do
33
- Dim::ExitHelper.exit(code: 0, msg: Dim::Ver.sion)
34
- end
35
- opts.on('-l', '--license', 'prints license') do
36
- Dim::ExitHelper.exit(code: 0, msg: File.read(File.dirname(__FILE__) + '/../../license.txt'))
37
- end
38
-
39
- opts.separator "\nFor check, export, stats, format:"
40
- opts.on('-i FILENAME', '--input FILENAME', 'input file or config') do |i|
41
- OPTIONS[:input] = i.gsub('\\', '/')
42
- end
43
- opts.on('-a FILENAME', '--attributes FILENAME', 'file for custom attributes') do |i|
44
- OPTIONS[:attributes] = i.gsub('\\', '/')
45
- end
46
- opts.on('--allow-missing', 'Missing references are ignored') do |_m|
47
- OPTIONS[:allow_missing] = true
48
- end
49
- opts.on('--no-check-enclosed', 'Skip enclosed file check') do |_|
50
- OPTIONS[:no_check_enclosed] = true
51
- end
52
- opts.on('--silent', 'Silent information log') do |_|
53
- OPTIONS[:silent] = true
54
- end
55
-
56
- opts.separator "\nFor export:"
57
- opts.on('--filter FILTER', 'searches for this string in all fields with enums') do |p|
58
- OPTIONS[:filter] = p
59
- end
60
-
61
- opts.separator "\nFor export:"
62
- opts.on('-o FOLDER', '--output FOLDER', 'output folder') do |p|
63
- OPTIONS[:folder] = p
64
- end
65
- opts.on('-f FORMAT', '--format FORMAT', 'output format',
66
- "allowed values: #{EXPORTER.keys.join(', ')}") do |p|
67
- OPTIONS[:type] =
68
- p
69
- end
70
-
71
- opts.separator "\nFor format:"
72
- opts.on('--output-format FORMAT', 'in-place (default): files will be changed if not already formatted correctly',
73
- 'extra: output is written into "<original filename>.formatted"',
74
- 'check-only: no changes to files',
75
- 'stdout: For IDE format support; will work with STDIN/STDOUT; no file input required') do |p|
76
- OPTIONS[:output_format] = p
77
- end
78
- end
79
-
80
- begin
81
- op.parse!(args)
82
- rescue OptionParser::InvalidOption => e
83
- Dim::ExitHelper.exit(code: 1, msg: e.message)
84
- end
85
-
86
- Dim::ExitHelper.exit(code: 1, msg: op) if args.empty? || !SUBCOMMANDS.keys.include?(args[0])
87
- OPTIONS[:subcommand] = args[0]
88
-
89
- Dim::ExitHelper.exit(code: 1, msg: 'no input file specified.') if OPTIONS[:input].nil? && OPTIONS[:output_format] != 'stdout'
90
-
91
- if OPTIONS[:subcommand] == 'export'
92
- Dim::ExitHelper.exit(code: 1, msg: 'specify output folder') if OPTIONS[:folder].nil?
93
- if OPTIONS[:type].nil?
94
- Dim::ExitHelper.exit(code: 1, msg: "export format not specified, must be one of: #{EXPORTER.keys.join(', ')}")
95
- end
96
- unless EXPORTER.keys.include?(OPTIONS[:type])
97
- Dim::ExitHelper.exit(code: 1, msg: "export format must be one of: #{EXPORTER.keys.join(', ')}")
98
- end
99
- end
100
-
101
- if OPTIONS[:output_format] == 'stdout'
102
- OPTIONS[:silent] = true
103
- OPTIONS[:no_check_enclosed] = true
104
- OPTIONS[:allow_missing] = true
105
- end
106
-
107
- if OPTIONS[:subcommand] == 'format'
108
- OPTIONS[:allow_missing] = true
109
-
110
- return if %w[in-place extra check-only stdout].include?(OPTIONS[:output_format])
111
-
112
- Dim::ExitHelper.exit(code: 1, msg: 'output-format must be in-place, extra or check-only')
113
- end
114
- end
115
- end
116
- end
1
+ require 'optparse'
2
+
3
+ require_relative 'globals'
4
+ require_relative 'exit_helper'
5
+
6
+ module Dim
7
+ class Options
8
+ MAX_COUNT = 10
9
+
10
+ def self.reset
11
+ OPTIONS[:filter] = ''
12
+ OPTIONS[:title] = 'Requirements - Draft'
13
+ OPTIONS[:folder] = nil
14
+ OPTIONS[:input] = nil
15
+ OPTIONS[:attributes] = nil
16
+ OPTIONS[:config] = nil
17
+ OPTIONS[:allow_missing] = false
18
+ OPTIONS[:output_format] = 'in-place'
19
+ OPTIONS[:type] = nil
20
+ OPTIONS[:no_check_enclosed] = false
21
+ OPTIONS[:silent] = nil
22
+ end
23
+ reset
24
+
25
+ def self.parse(args = ARGV)
26
+ op = OptionParser.new do |opts|
27
+ opts.banner = "Usage: dim.rb <#{SUBCOMMANDS.keys.join('|')}> [options]"
28
+ opts.separator "\nGeneral options:"
29
+ opts.on('-h', '--help', 'prints this help') do
30
+ Dim::ExitHelper.exit(code: 0, msg: opts)
31
+ end
32
+ opts.on('-v', '--version', 'prints version') do
33
+ Dim::ExitHelper.exit(code: 0, msg: Dim::Ver.sion)
34
+ end
35
+ opts.on('-l', '--license', 'prints license') do
36
+ Dim::ExitHelper.exit(code: 0, msg: File.read(File.dirname(__FILE__) + '/../../license.txt'))
37
+ end
38
+
39
+ opts.separator "\nFor check, export, stats, format:"
40
+ opts.on('-i FILENAME', '--input FILENAME', 'input file or config') do |i|
41
+ OPTIONS[:input] = i.gsub('\\', '/')
42
+ end
43
+ opts.on('-a FILENAME', '--attributes FILENAME', 'file for custom attributes') do |i|
44
+ OPTIONS[:attributes] = i.gsub('\\', '/')
45
+ end
46
+ opts.on('--allow-missing', 'Missing references are ignored') do |_m|
47
+ OPTIONS[:allow_missing] = true
48
+ end
49
+ opts.on('--no-check-enclosed', 'Skip enclosed file check') do |_|
50
+ OPTIONS[:no_check_enclosed] = true
51
+ end
52
+ opts.on('--silent', 'Silent information log') do |_|
53
+ OPTIONS[:silent] = true
54
+ end
55
+
56
+ opts.separator "\nFor export:"
57
+ opts.on('--filter FILTER', 'searches for this string in all fields with enums') do |p|
58
+ OPTIONS[:filter] = p
59
+ end
60
+
61
+ opts.separator "\nFor export:"
62
+ opts.on('-o FOLDER', '--output FOLDER', 'output folder') do |p|
63
+ OPTIONS[:folder] = p
64
+ end
65
+ opts.on('-f FORMAT', '--format FORMAT', 'output format',
66
+ "allowed values: #{EXPORTER.keys.join(', ')}") do |p|
67
+ OPTIONS[:type] =
68
+ p
69
+ end
70
+
71
+ opts.separator "\nFor format:"
72
+ opts.on('--output-format FORMAT', 'in-place (default): files will be changed if not already formatted correctly',
73
+ 'extra: output is written into "<original filename>.formatted"',
74
+ 'check-only: no changes to files',
75
+ 'stdout: For IDE format support; will work with STDIN/STDOUT; no file input required') do |p|
76
+ OPTIONS[:output_format] = p
77
+ end
78
+ end
79
+
80
+ begin
81
+ op.parse!(args)
82
+ rescue OptionParser::InvalidOption => e
83
+ Dim::ExitHelper.exit(code: 1, msg: e.message)
84
+ end
85
+
86
+ Dim::ExitHelper.exit(code: 1, msg: op) if args.empty? || !SUBCOMMANDS.keys.include?(args[0])
87
+ OPTIONS[:subcommand] = args[0]
88
+
89
+ Dim::ExitHelper.exit(code: 1, msg: 'no input file specified.') if OPTIONS[:input].nil? && OPTIONS[:output_format] != 'stdout'
90
+
91
+ if OPTIONS[:subcommand] == 'export'
92
+ Dim::ExitHelper.exit(code: 1, msg: 'specify output folder') if OPTIONS[:folder].nil?
93
+ if OPTIONS[:type].nil?
94
+ Dim::ExitHelper.exit(code: 1, msg: "export format not specified, must be one of: #{EXPORTER.keys.join(', ')}")
95
+ end
96
+ unless EXPORTER.keys.include?(OPTIONS[:type])
97
+ Dim::ExitHelper.exit(code: 1, msg: "export format must be one of: #{EXPORTER.keys.join(', ')}")
98
+ end
99
+ end
100
+
101
+ if OPTIONS[:output_format] == 'stdout'
102
+ OPTIONS[:silent] = true
103
+ OPTIONS[:no_check_enclosed] = true
104
+ OPTIONS[:allow_missing] = true
105
+ end
106
+
107
+ if OPTIONS[:subcommand] == 'format'
108
+ OPTIONS[:allow_missing] = true
109
+
110
+ return if %w[in-place extra check-only stdout].include?(OPTIONS[:output_format])
111
+
112
+ Dim::ExitHelper.exit(code: 1, msg: 'output-format must be in-place, extra or check-only')
113
+ end
114
+ end
115
+ end
116
+ end