appl 1.5.1 → 1.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7ca5c3e7d0c0283079f9bd0981807b994cc1ea40bdf39830e14ddc7139f6e2e3
4
- data.tar.gz: dba6e93d8c2dba17713ae40e0b828ca2ad8f2b9a526b8024f3f6ec30ffd3a6bc
3
+ metadata.gz: 1688b3e222470fd0e8d460eb628e5cd0d367f6310657f55fe465f004b7d09974
4
+ data.tar.gz: 2377f5d01f50f75922cd10fef21d17ef123cfec1d8bce0cf8873283cd73df2ac
5
5
  SHA512:
6
- metadata.gz: cabd022a9b45ef68fe54ebe962d102c6fef398dd816a811c0647157f474bb7d90cf2b7c86558b384ce5f2946c5e801b0eaed4f22168afd976efdd580e93f1a20
7
- data.tar.gz: 00ff5f113cbfc1fdf390ba5790131c72a2e672c8651e506fa88eea8ffe67e8b2065047f1c911b290279cb9f6148bb1ebfea24f937dfd18c766f75a22f5b92316
6
+ metadata.gz: 4c4a1b091ac1e4bdab240d55627eb78811fddeb0962b30aef7185f2fe466809a3d8cbf847387b1aa1ab07e782770e7b4e3507821682557e7388915d2cc1b1056
7
+ data.tar.gz: ee2e1d53b6f6c32a8be5d48e7f1489104d3f316d58a67f82318f0f21b16d038676812a063d5500d80663050963d086ad9bbcdff00b810d299d3ac731c0abba3b
data/bin/intar CHANGED
@@ -17,44 +17,21 @@ class IntarApp < Application
17
17
  LICENSE = "BSD"
18
18
  AUTHOR = "Bertram Scharpf <software@bertram-scharpf.de>"
19
19
 
20
- DESCRIPTION = <<EOT
21
- Prompt for Ruby statements, evaluate them. This is a replacement
22
- for "irb". The underlying library may be entered from inside any
23
- Ruby program.
20
+ DESCRIPTION = <<~EOT
21
+ Prompt for Ruby statements, evaluate them. This is a replacement
22
+ for "irb". The underlying library may be entered from inside any
23
+ Ruby program.
24
24
 
25
- Example:
25
+ Example:
26
26
 
27
- $ intar -p '%(33 1)c%t%c%> '
27
+ $ intar -p '%(33 1)c%t%c%> '
28
28
 
29
- EOT
29
+ EOT
30
30
 
31
- def quiet!
32
- Intar.show = nil
33
- end
34
- def show= s
35
- Intar.show = s.to_i
36
- end
37
- def prompt= p
38
- Intar.prompt = p if p
39
- end
40
- def bw!
41
- Intar.colour = false
42
- end
43
- def catch_exit!
44
- Intar.catch_exit = (Intar.catch_exit||0) + 3
45
- end
46
- def histall!
47
- Intar.histhid = false
48
- end
49
- def histfile= h
50
- Intar.histfile = nil_if_none h
51
- end
52
- def histmax= m
53
- Intar.histmax = (Integer m rescue m.to_i)
54
- end
55
- def configfile= c
56
- @configfile = nil_if_none c
57
- end
31
+ attr_writer :show, :prompt, :histmax
32
+ def histfile= f ; @histfile = nil_if_none f ; end
33
+ def configfile= f ; @configfile = nil_if_none f ; end
34
+ attr_bang :quiet, :bw, :catch_exit, :histall
58
35
 
59
36
  define_option "p", :prompt=, "STR",
60
37
  "prompt - see source code for % escapes"
@@ -63,8 +40,8 @@ EOT
63
40
  define_option "q", :quiet!, "don't show results"
64
41
  alias_option "q", "quiet"
65
42
 
66
- define_option "S", :show=, "N", 1, "show result line limit (0=all)"
67
- alias_option "S", "show"
43
+ define_option "s", :show=, "N", "show result line limit (0=all)"
44
+ alias_option "s", "show"
68
45
 
69
46
  define_option "r", :require, "FILE", "Ruby require"
70
47
  alias_option "r", "require"
@@ -75,7 +52,7 @@ EOT
75
52
  "config file, NONE means none"
76
53
  alias_option "c", "configfile"
77
54
 
78
- define_option "H", :histfile=, "FILE", ".intar_history",
55
+ define_option "H", :histfile=, "FILE",
79
56
  "history file, NONE means none"
80
57
  alias_option "H", "histfile"
81
58
 
@@ -103,6 +80,18 @@ EOT
103
80
  def run
104
81
  # @debug = true # Only development.
105
82
  read_cfg
83
+ if @quiet then
84
+ Intar.show = 0
85
+ elsif @show then
86
+ Intar.show = Integer @show
87
+ end
88
+ Intar.prompt = @prompt if @prompt
89
+ Intar.colour = false if @bw
90
+ Intar.catch_exit = (Intar.catch_exit||0) + 3 if @catch_exit
91
+ Intar.histhid = false if @histall
92
+ Intar.histfile = @histfile if @histfile
93
+ Intar.histmax = Integer @histmax if @histmax
94
+
106
95
  main = eval "self", TOPLEVEL_BINDING
107
96
  Intar.open main do |i|
108
97
  i.run *(@args.shift @args.length)
@@ -15,11 +15,10 @@ class DemoAppl < Application
15
15
  LICENSE = "For internal use only"
16
16
  AUTHOR = "Bertram Scharpf <software@bertram-scharpf.de>"
17
17
 
18
- DESCRIPTION = <<EOT
19
- This is an example how to define an Application subclass.
20
- The program just prints out its parameters.
21
-
22
- EOT
18
+ DESCRIPTION = <<~EOT
19
+ This is an example how to define an Application subclass.
20
+ The program just prints out its parameters.
21
+ EOT
23
22
 
24
23
  OPTIONS_ENV = "DEMO_OPTS"
25
24
 
@@ -47,9 +46,9 @@ EOT
47
46
  define_option "V", :version, "show version information"
48
47
  alias_option "V", "version"
49
48
 
50
- UNKNOWN = "Sorry, unknown option"
51
- STOPOPT = "no more options"
52
- UNPROCA = "Warning: unprocessed arguments left"
49
+ STOPOPT = "Stop option processing"
50
+ UNKNOWN = "Not a valid option: `%s'."
51
+ UNPROCA = "Warning. Unprocessed arguments: %s"
53
52
 
54
53
  def run
55
54
  puts inspect
@@ -0,0 +1,90 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #
4
+ # demofan -- Test the Application class with commands
5
+ #
6
+
7
+ require "applfan"
8
+
9
+
10
+ class DemoFan < ApplicationFan
11
+
12
+ NAME = "demofan"
13
+ VERSION = "1.0"
14
+ SUMMARY = "Test the Application class with commands"
15
+ COPYRIGHT = "(C) 2020 Bertram Scharpf"
16
+ LICENSE = "For internal use only"
17
+ AUTHOR = "Bertram Scharpf <software@bertram-scharpf.de>"
18
+
19
+ DESCRIPTION = <<~EOT
20
+ This is an example how to define an Application subclass
21
+ with commands inside.
22
+ EOT
23
+
24
+ attr_bang :verbose
25
+ define_option "v", :verbose!, "verbose mode"
26
+
27
+ define_option "h", :help, "show this options list"
28
+ alias_option "h", "help"
29
+ define_option "V", :version, "show version information"
30
+ alias_option "V", "version"
31
+
32
+ def run
33
+ super do |cmd|
34
+ cmd.verbose! if @verbose
35
+ end
36
+ end
37
+
38
+ class Init < Command
39
+ NAME = "init"
40
+ SUMMARY = "Initialize"
41
+ DESCRIPTION = <<~EOT
42
+ Initialization of nothing.
43
+ EOT
44
+ define_option "h", :help, "show init's options list"
45
+ alias_option "h", "help"
46
+ def verbose! ; end
47
+ def run
48
+ puts "Initializing...done."
49
+ end
50
+ end
51
+
52
+ class Run < Command
53
+ NAME = "run"
54
+ SUMMARY = "Run a job"
55
+ DESCRIPTION = <<~EOT
56
+ Running nothing.
57
+ EOT
58
+ define_option "h", :help, "show run's options list"
59
+ alias_option "h", "help"
60
+ def verbose! ; @verbose = true ; end
61
+ def run
62
+ if @verbose then
63
+ puts "Starting..."
64
+ puts "Running..."
65
+ puts "Done."
66
+ else
67
+ puts "Running...done."
68
+ end
69
+ end
70
+ end
71
+
72
+ class Cleanup < Command
73
+ NAME = "cleanup"
74
+ SUMMARY = "Cleanup"
75
+ DESCRIPTION = <<~EOT
76
+ Cleanup of nothing.
77
+ EOT
78
+ define_option "h", :help, "show cleanup's options list"
79
+ alias_option "h", "help"
80
+ def verbose! ; end
81
+ def run
82
+ puts "Cleaning up...done."
83
+ end
84
+ end
85
+
86
+ end
87
+
88
+
89
+ DemoFan.run
90
+
@@ -5,13 +5,16 @@
5
5
 
6
6
  class Application
7
7
 
8
- APPL_VERSION = "1.5.1".freeze
8
+ APPL_VERSION = "1.10".freeze
9
9
 
10
10
  OPTIONS_ENV = nil
11
11
 
12
12
  STOPOPT = "stop option processing"
13
- UNKNOWN = "Unknown option"
14
- UNPROCA = "Warning: unprocessed arguments"
13
+ UNKNOWN = "Unknown option: `%s'."
14
+ UNPROCA = "Warning: unprocessed arguments: %s"
15
+
16
+ W_OPTS = 10
17
+ W_ARGS = 16
15
18
 
16
19
  class OptionError < StandardError ; end
17
20
  class Done < Exception ; end
@@ -46,45 +49,33 @@ class Application
46
49
  end
47
50
 
48
51
  def help
49
- c = self.class
50
- puts c.version
51
- puts
52
- puts c::DESCRIPTION
53
- puts
54
- c.show_options
55
- if block_given? then
56
- puts
57
- yield
58
- end
52
+ self.class.help
59
53
  raise Done
60
54
  end
61
55
 
62
56
  def version
63
- self.class.show_version
57
+ self.class.version
64
58
  raise Done
65
59
  end
66
60
 
67
- VERSION = "0"
68
- NAME = "appl"
69
- SUMMARY = "Dummy application"
70
- DESCRIPTION = <<-EOT
71
- This base class does nothing by default.
72
- EOT
73
-
74
61
  def run
75
62
  end
76
63
 
77
- def execute
78
- run
64
+ def warn_unprocessed
79
65
  if @args.any? then
80
- u = @args.join " "
81
- puts "#{self.class::UNPROCA}: #{u}"
66
+ $stderr.puts self.class.root::UNPROCA % (@args.join " ")
82
67
  end
83
- 0
68
+ end
69
+
70
+ def execute
71
+ r = run
72
+ r = 0 unless Integer === r
73
+ warn_unprocessed
74
+ r
84
75
  rescue SignalException
85
76
  raise if @debug
86
77
  self.class.show_message $!.inspect
87
- 128 + ($!.signo||2) # Ruby 1.8 returns signo +nil+; assume SIGINT
78
+ 128 + $!.signo
88
79
  rescue
89
80
  raise if @debug
90
81
  self.class.show_message "Error: #$!", "(#{$!.class})"
@@ -98,14 +89,6 @@ This base class does nothing by default.
98
89
  exit e
99
90
  end
100
91
 
101
- def version
102
- if self::VERSION =~ %r/\s/ then
103
- self::VERSION
104
- else
105
- "#{self::NAME} #{self::VERSION} -- #{self::SUMMARY}"
106
- end
107
- end
108
-
109
92
  private
110
93
 
111
94
  def execute args = nil
@@ -119,7 +102,9 @@ This base class does nothing by default.
119
102
  end
120
103
 
121
104
  def inherited sub
122
- sub.instance_eval { @options, @aliases = {}, {} }
105
+ sub.name or return
106
+ o, a = @options.dup.to_h, @aliases.dup.to_h
107
+ sub.instance_eval { @options, @aliases = o, a }
123
108
  end
124
109
 
125
110
  def attr_bang *syms
@@ -162,16 +147,12 @@ This base class does nothing by default.
162
147
  end
163
148
 
164
149
  def delete_option opt
165
- self < Application or return
166
- superclass.delete_option opt
167
- @options.delete opt
168
150
  @aliases.reject! { |k,v| v == opt }
151
+ @options.delete opt
169
152
  nil
170
153
  end
171
154
 
172
155
  def unalias_option opt
173
- self < Application or return
174
- superclass.unalias_option opt
175
156
  @aliases.delete opt
176
157
  nil
177
158
  end
@@ -179,32 +160,12 @@ This base class does nothing by default.
179
160
  protected
180
161
 
181
162
  def find_option_act opt
182
- self < Application or return
183
- @options[ opt] || @options[ @aliases[ opt]] ||
184
- (superclass.find_option_act opt)
185
- end
186
-
187
- def all_options
188
- if self < Application then
189
- r = superclass.all_options
190
- r.update @options
191
- else
192
- {}
193
- end
194
- end
195
-
196
- def all_aliases
197
- if self < Application then
198
- r = superclass.all_aliases
199
- r.update @aliases
200
- else
201
- {}
202
- end
163
+ @options[ opt] || @options[ @aliases[ opt]]
203
164
  end
204
165
 
205
166
  def options_desc &block
206
167
  a = Hash.new do |h,k| h[ k] = [] end
207
- all_aliases.each { |k,v|
168
+ @aliases.each { |k,v|
208
169
  a[ v].push k
209
170
  }
210
171
  each_option { |opt,desc,arg,dfl,|
@@ -213,14 +174,13 @@ This base class does nothing by default.
213
174
  yield l, nil, nil, nil
214
175
  }
215
176
  }
216
- yield "", nil, nil, self::STOPOPT
177
+ yield "", nil, nil, root::STOPOPT
217
178
  end
218
179
 
219
180
  public
220
181
 
221
182
  def each_option
222
- o = all_options
223
- o.each { |opt,(desc,arg,dfl,act)|
183
+ @options.each { |opt,(desc,arg,dfl,act)|
224
184
  case dfl
225
185
  when Symbol then dfl = const_get dfl
226
186
  end
@@ -230,7 +190,7 @@ This base class does nothing by default.
230
190
 
231
191
  def option_act args, opt, rest
232
192
  dada = find_option_act opt
233
- dada or raise OptionError, "#{self::UNKNOWN}: `#{opt}'."
193
+ dada or raise OptionError, root::UNKNOWN % opt
234
194
  desc, arg, dfl, act = *dada
235
195
  r = [ act]
236
196
  if arg then
@@ -246,12 +206,32 @@ This base class does nothing by default.
246
206
  arg &&= "#{arg}"
247
207
  dfl &&= "[#{dfl}]"
248
208
  arg << dfl if arg && dfl
249
- puts " %-10s %-12s %s" % [ opt, arg, desc]
209
+ puts " %-*s %-*s %s" % [ root::W_OPTS, opt, root::W_ARGS, arg, desc]
210
+ end
211
+ end
212
+
213
+ def root
214
+ self
215
+ end
216
+
217
+ def applname
218
+ self::NAME
219
+ end
220
+
221
+ def help
222
+ puts "#{applname} -- #{self::SUMMARY}"
223
+ puts
224
+ puts self::DESCRIPTION
225
+ puts
226
+ show_options
227
+ if block_given? then
228
+ puts
229
+ yield
250
230
  end
251
231
  end
252
232
 
253
- def show_version
254
- puts version
233
+ def version
234
+ puts "#{applname} #{self::VERSION} -- #{self::SUMMARY}"
255
235
  puts self::COPYRIGHT if const_defined? :COPYRIGHT
256
236
  puts "License: #{self::LICENSE}" if const_defined? :LICENSE
257
237
  a = []
@@ -264,6 +244,7 @@ This base class does nothing by default.
264
244
  h = a.length == 1 ? "Author" : "Authors"
265
245
  puts "#{h}: #{j}"
266
246
  end
247
+ puts "Ruby version: #{RUBY_VERSION}"
267
248
  end
268
249
 
269
250
  def show_message msg, extra = nil
@@ -0,0 +1,79 @@
1
+ #
2
+ # lib/applfan.rb -- ApplicationFan class
3
+ #
4
+
5
+ require "appl"
6
+
7
+
8
+ ApplicationFan = Class.new Application
9
+
10
+ class ApplicationFan
11
+
12
+ AVAILCMDS = "Available commands (say -h after one for help)"
13
+ NOCOMMAND = "No command given. Say -h for a list."
14
+ UNKNWNCMD = "Unknown command: `%s'. Say -h for a list."
15
+
16
+ W_CMDS = 16
17
+
18
+ class CommandError < StandardError ; end
19
+
20
+ class <<self
21
+
22
+ attr_accessor :commands
23
+
24
+ def find_command name
25
+ @commands.find { |c| c::NAME == name }
26
+ end
27
+
28
+
29
+ def help
30
+ super do
31
+ if block_given? then
32
+ yield
33
+ puts
34
+ end
35
+ puts self::AVAILCMDS
36
+ puts
37
+ @commands.each { |c|
38
+ puts " %-*s %s" % [ self::W_CMDS, c::NAME, c::SUMMARY]
39
+ }
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def inherited sub
46
+ sub.instance_eval do
47
+ @commands = []
48
+ const_set :Command, (
49
+ Class.new Application do
50
+ define_singleton_method :inherited do |c|
51
+ sub.commands.push c
52
+ super c
53
+ end
54
+ define_singleton_method :root do
55
+ sub
56
+ end
57
+ define_singleton_method :applname do
58
+ "#{root::NAME} #{self::NAME}"
59
+ end
60
+ end
61
+ )
62
+ end
63
+ super
64
+ end
65
+
66
+ end
67
+
68
+ def run
69
+ c = @args.shift
70
+ c or raise CommandError, self.class::NOCOMMAND
71
+ cmd = self.class.find_command c
72
+ cmd or raise CommandError, self.class::UNKNWNCMD % c
73
+ sub = cmd.new @args.slice! 0, @args.length
74
+ yield sub if block_given?
75
+ sub.execute
76
+ end
77
+
78
+ end
79
+
@@ -128,7 +128,7 @@ class Intar
128
128
 
129
129
  class <<self
130
130
 
131
- attr_accessor :prompt, :show, :colour
131
+ attr_accessor :prompt, :show, :shownil, :colour
132
132
 
133
133
  attr_reader :histfile
134
134
  def histfile= hf
@@ -158,6 +158,7 @@ class Intar
158
158
  s = superclass
159
159
  @prompt = s.prompt
160
160
  @show = s.show
161
+ @shownil = s.shownil
161
162
  @colour = s.colour
162
163
  @histfile = s.histfile
163
164
  @histmax = s.histmax
@@ -206,6 +207,7 @@ class Intar
206
207
 
207
208
  self.prompt = "%(32)c%i%c:%1c%03n%c%> "
208
209
  self.show = 1
210
+ self.shownil = false
209
211
  self.colour = true
210
212
  self.histfile = nil
211
213
  self.histmax = 500
@@ -221,7 +223,7 @@ class Intar
221
223
  @n = 0
222
224
  end
223
225
 
224
- OLDSET = <<-EOT
226
+ OLDSET = <<~EOT
225
227
  _, __, ___ = nil, nil, nil
226
228
  proc { |r,n|
227
229
  Array === __ or __ = []
@@ -312,19 +314,18 @@ class Intar
312
314
  # :startdoc:
313
315
 
314
316
  def display r
315
- return if r.nil?
317
+ return if r.nil? and not self.class.shownil
316
318
  show = (self.class.show or return)
317
- i = r.inspect
319
+ i = ARROW.dup
320
+ i << r.inspect
318
321
  if show > 0 then
319
322
  siz, = $stdout.winsize
320
323
  siz *= show
321
- siz -= ARROW.length
322
324
  if i.length > siz then
323
325
  i.cut! siz-ELLIPSIS.length
324
326
  i << ELLIPSIS
325
327
  end
326
328
  end
327
- i.prepend ARROW
328
329
  puts i
329
330
  end
330
331
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: appl
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.1
4
+ version: '1.10'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bertram Scharpf
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-13 00:00:00.000000000 Z
11
+ date: 2020-06-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  A base class for command line applications doing options parsing
@@ -21,13 +21,15 @@ extra_rdoc_files: []
21
21
  files:
22
22
  - bin/intar
23
23
  - doc/demoappl
24
+ - doc/demofan
24
25
  - lib/appl.rb
26
+ - lib/applfan.rb
25
27
  - lib/intar.rb
26
28
  homepage: http://www.bertram-scharpf.de/software/appl
27
29
  licenses:
28
30
  - BSD-2-Clause
29
31
  metadata: {}
30
- post_install_message:
32
+ post_install_message:
31
33
  rdoc_options: []
32
34
  require_paths:
33
35
  - lib
@@ -43,9 +45,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
43
45
  version: '0'
44
46
  requirements:
45
47
  - Just Ruby
46
- rubyforge_project: NONE
47
- rubygems_version: 2.7.8
48
- signing_key:
48
+ rubygems_version: 3.0.6
49
+ signing_key:
49
50
  specification_version: 4
50
51
  summary: Easy option parsing
51
52
  test_files: []