shellopts 2.0.0.pre.13 → 2.0.0.pre.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,123 +0,0 @@
1
-
2
- require "shellopts"
3
-
4
- require "shellopts/args.rb"
5
-
6
- # TODO
7
- #
8
- # PROCESSING
9
- # 1. Compile spec string and yield a grammar
10
- # 2. Parse the options using the grammar and yield an AST
11
- # 3. Construct the Program model from the AST
12
- # 4. Apply defaults to the model
13
- # 6. Run validations on the model
14
- # 5. Create representation from the model
15
- #
16
-
17
- module ShellOpts
18
- # The command line processing object
19
- class ShellOpts
20
- # Name of program
21
- attr_accessor :name
22
-
23
- # Usage string. If #usage is nil, the auto-generated default is used
24
- def usage() @usage || @grammar.usage end
25
- def usage=(usage) @usage = usage end
26
-
27
- # Specification of the command
28
- attr_reader :spec
29
-
30
- # Original argv argument
31
- attr_reader :argv
32
-
33
- # The grammar compiled from the spec string
34
- attr_reader :grammar
35
-
36
- # The AST parsed from the command line arguments
37
- attr_reader :ast
38
-
39
- # The IDR generated from the Ast
40
- attr_reader :idr
41
-
42
- # Compile a spec string into a grammar
43
- #
44
- # +spec+ is the spec string, and +argv+ the command line (typically the
45
- # global ARGV array). +name+ is the name of the program and defaults to the
46
- # basename of the program
47
- #
48
- # Syntax errors in the spec string are caused by the developer and cause
49
- # #initialize to raise a +ShellOpts::CompilerError+ exception. Errors in
50
- # the +argv+ arguments are caused by the user and cause #process to raise
51
- # ShellOpts::UserError exception
52
- #
53
- # TODO: Change to (name, spec, argv, usage: nil) because
54
- # ShellOpts::ShellOpts isn't a magician like the ShellOpts module
55
- def initialize(spec, argv, name: ::ShellOpts.default_name, usage: ::ShellOpts.default_usage)
56
- @name = name
57
- @spec = spec
58
- @usage = usage
59
- @argv = argv
60
- begin
61
- @grammar = Grammar.compile(@name, @spec)
62
- rescue Grammar::Compiler::Error => ex
63
- raise CompilerError.new(5, ex.message)
64
- end
65
- end
66
-
67
- # Process command line arguments and return self. Raises a
68
- # ShellOpts::UserError in case of an error
69
- def process
70
- begin
71
- @ast = Ast.parse(@grammar, @argv)
72
- @idr = Idr.generate(self)
73
- rescue Ast::Parser::Error => ex
74
- raise UserError.new(ex.message)
75
- end
76
- self
77
- end
78
-
79
- # Return an array representation of options and commands in the same order
80
- # as on the command line. Each option or command is represented by a [name,
81
- # value] pair. The value of an option is be nil if the option didn't have
82
- # an argument and else either a String, Integer, or Float. The value of a
83
- # command is an array of its options and commands
84
- def to_a() idr.to_a end
85
-
86
- # Return a hash representation of the options. See {ShellOpts::OptionsHash}
87
- def to_h(key_type: ::ShellOpts.default_key_type, aliases: {})
88
- @idr.to_h(key_type: :key_type, aliases: aliases)
89
- end
90
-
91
- # TODO
92
- # Return OptionHash object
93
- # def to_hash(...)
94
-
95
- # Return a struct representation of the options. See {ShellOpts::OptionStruct}
96
- def to_struct(key_type: ::ShellOpts.default_key_type, aliases: {})
97
- @idr.to_struct(key_type: key_type, aliases: aliases)
98
- end
99
-
100
- # List of remaining non-option command line arguments. Returns a Argv object
101
- def args() Args.new(self, ast&.arguments) end
102
-
103
- # Iterate options and commands as name/value pairs. Same as +to_a.each+
104
- def each(&block) to_a.each(&block) end
105
-
106
- # Print error messages and spec string and exit with status 1. This method
107
- # should be called in response to user-errors (eg. specifying an illegal
108
- # option)
109
- def error(*msgs, exit: true)
110
- msg = "#{name}: #{msgs.join}\n" + (@usage ? usage : "Usage: #{name} #{usage}")
111
- $stderr.puts msg.rstrip
112
- exit(1) if exit
113
- end
114
-
115
- # Print error message and exit with status 1. This method should called in
116
- # response to system errors (like disk full)
117
- def fail(*msgs, exit: true)
118
- $stderr.puts "#{name}: #{msgs.join}"
119
- exit(1) if exit
120
- end
121
- end
122
- end
123
-