alf-shell 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (189) hide show
  1. data/CHANGELOG.md +4 -0
  2. data/Gemfile +17 -0
  3. data/Gemfile.lock +35 -0
  4. data/LICENCE.md +22 -0
  5. data/Manifest.txt +13 -0
  6. data/README.md +11 -0
  7. data/Rakefile +11 -0
  8. data/bin/alf +17 -0
  9. data/lib/alf-shell.rb +1 -0
  10. data/lib/alf/shell.rb +48 -0
  11. data/lib/alf/shell/command.rb +21 -0
  12. data/lib/alf/shell/command/exec.rb +16 -0
  13. data/lib/alf/shell/command/help.rb +30 -0
  14. data/lib/alf/shell/command/main.rb +175 -0
  15. data/lib/alf/shell/command/show.rb +49 -0
  16. data/lib/alf/shell/doc_manager.rb +83 -0
  17. data/lib/alf/shell/ext/signature.rb +45 -0
  18. data/lib/alf/shell/from_argv.rb +78 -0
  19. data/lib/alf/shell/loader.rb +2 -0
  20. data/lib/alf/shell/operator.rb +63 -0
  21. data/lib/alf/shell/support.rb +28 -0
  22. data/lib/alf/shell/version.rb +16 -0
  23. data/spec/integration/__database__/group.alf +3 -0
  24. data/spec/integration/__database__/parts.rash +6 -0
  25. data/spec/integration/__database__/suppliers.rash +5 -0
  26. data/spec/integration/__database__/suppliers_csv.csv +6 -0
  27. data/spec/integration/__database__/supplies.rash +12 -0
  28. data/spec/integration/alf/alf.db +0 -0
  29. data/spec/integration/alf/alf_db.cmd +1 -0
  30. data/spec/integration/alf/alf_db.stdout +5 -0
  31. data/spec/integration/alf/alf_e.cmd +1 -0
  32. data/spec/integration/alf/alf_e.stdout +4 -0
  33. data/spec/integration/alf/alf_help.cmd +1 -0
  34. data/spec/integration/alf/alf_help.stdout +72 -0
  35. data/spec/integration/alf/alf_json.cmd +1 -0
  36. data/spec/integration/alf/alf_json.stdout +1 -0
  37. data/spec/integration/alf/alf_r.cmd +1 -0
  38. data/spec/integration/alf/alf_r.stdout +5 -0
  39. data/spec/integration/alf/alf_version.cmd +1 -0
  40. data/spec/integration/alf/alf_version.stdout +2 -0
  41. data/spec/integration/alf/alf_yaml.cmd +1 -0
  42. data/spec/integration/alf/alf_yaml.stdout +22 -0
  43. data/spec/integration/alf/rel.rash +1 -0
  44. data/spec/integration/autonum/autonum_0.cmd +1 -0
  45. data/spec/integration/autonum/autonum_0.stdout +9 -0
  46. data/spec/integration/autonum/autonum_1.cmd +1 -0
  47. data/spec/integration/autonum/autonum_1.stdout +9 -0
  48. data/spec/integration/clip/clip_0.cmd +1 -0
  49. data/spec/integration/clip/clip_0.stdout +9 -0
  50. data/spec/integration/clip/clip_1.cmd +1 -0
  51. data/spec/integration/clip/clip_1.stdout +9 -0
  52. data/spec/integration/coerce/coerce_1.cmd +1 -0
  53. data/spec/integration/coerce/coerce_1.stdout +5 -0
  54. data/spec/integration/compact/compact_0.cmd +1 -0
  55. data/spec/integration/compact/compact_0.stdout +9 -0
  56. data/spec/integration/defaults/defaults_0.cmd +1 -0
  57. data/spec/integration/defaults/defaults_0.stdout +9 -0
  58. data/spec/integration/defaults/defaults_1.cmd +1 -0
  59. data/spec/integration/defaults/defaults_1.stdout +9 -0
  60. data/spec/integration/defaults/defaults_2.cmd +1 -0
  61. data/spec/integration/defaults/defaults_2.stdout +9 -0
  62. data/spec/integration/extend/extend_0.cmd +1 -0
  63. data/spec/integration/extend/extend_0.stdout +16 -0
  64. data/spec/integration/generator/generator_1.cmd +1 -0
  65. data/spec/integration/generator/generator_1.stdout +10 -0
  66. data/spec/integration/generator/generator_2.cmd +1 -0
  67. data/spec/integration/generator/generator_2.stdout +5 -0
  68. data/spec/integration/generator/generator_3.cmd +1 -0
  69. data/spec/integration/generator/generator_3.stdout +5 -0
  70. data/spec/integration/group/group_0.cmd +1 -0
  71. data/spec/integration/group/group_0.stdout +32 -0
  72. data/spec/integration/group/group_1.cmd +1 -0
  73. data/spec/integration/group/group_1.stdout +32 -0
  74. data/spec/integration/help/help_1.cmd +1 -0
  75. data/spec/integration/help/help_1.stdout +25 -0
  76. data/spec/integration/intersect/intersect_0.cmd +1 -0
  77. data/spec/integration/intersect/intersect_0.stdout +9 -0
  78. data/spec/integration/join/join_0.cmd +1 -0
  79. data/spec/integration/join/join_0.stdout +16 -0
  80. data/spec/integration/matching/matching_0.cmd +1 -0
  81. data/spec/integration/matching/matching_0.stdout +8 -0
  82. data/spec/integration/minus/minus_0.cmd +1 -0
  83. data/spec/integration/minus/minus_0.stdout +4 -0
  84. data/spec/integration/not-matching/not-matching_0.cmd +1 -0
  85. data/spec/integration/not-matching/not-matching_0.stdout +5 -0
  86. data/spec/integration/project/project_0.cmd +1 -0
  87. data/spec/integration/project/project_0.stdout +9 -0
  88. data/spec/integration/project/project_1.cmd +1 -0
  89. data/spec/integration/project/project_1.stdout +9 -0
  90. data/spec/integration/quota/quota_0.cmd +1 -0
  91. data/spec/integration/quota/quota_0.stdout +16 -0
  92. data/spec/integration/rank/rank_1.cmd +1 -0
  93. data/spec/integration/rank/rank_1.stdout +10 -0
  94. data/spec/integration/rank/rank_2.cmd +1 -0
  95. data/spec/integration/rank/rank_2.stdout +10 -0
  96. data/spec/integration/rank/rank_3.cmd +1 -0
  97. data/spec/integration/rank/rank_3.stdout +10 -0
  98. data/spec/integration/rank/rank_4.cmd +1 -0
  99. data/spec/integration/rank/rank_4.stdout +6 -0
  100. data/spec/integration/rank/rank_5.cmd +1 -0
  101. data/spec/integration/rank/rank_5.stdout +6 -0
  102. data/spec/integration/rename/rename_0.cmd +1 -0
  103. data/spec/integration/rename/rename_0.stdout +9 -0
  104. data/spec/integration/restrict/restrict_0.cmd +1 -0
  105. data/spec/integration/restrict/restrict_0.stdout +6 -0
  106. data/spec/integration/restrict/restrict_1.cmd +1 -0
  107. data/spec/integration/restrict/restrict_1.stdout +6 -0
  108. data/spec/integration/show/show_base.cmd +1 -0
  109. data/spec/integration/show/show_base.stdout +5 -0
  110. data/spec/integration/show/show_base_sort_1.cmd +1 -0
  111. data/spec/integration/show/show_base_sort_1.stdout +9 -0
  112. data/spec/integration/show/show_base_sort_2.cmd +1 -0
  113. data/spec/integration/show/show_base_sort_2.stdout +9 -0
  114. data/spec/integration/show/show_conflictual.cmd +1 -0
  115. data/spec/integration/show/show_conflictual.stdout +5 -0
  116. data/spec/integration/show/show_ff.cmd +1 -0
  117. data/spec/integration/show/show_ff.stdout +10 -0
  118. data/spec/integration/show/show_rash.cmd +1 -0
  119. data/spec/integration/show/show_rash.stdout +5 -0
  120. data/spec/integration/show/show_rash_pretty.cmd +1 -0
  121. data/spec/integration/show/show_rash_pretty.stdout +30 -0
  122. data/spec/integration/show/show_yaml.cmd +1 -0
  123. data/spec/integration/show/show_yaml.stdout +22 -0
  124. data/spec/integration/sort/sort_0.cmd +1 -0
  125. data/spec/integration/sort/sort_0.stdout +9 -0
  126. data/spec/integration/sort/sort_1.cmd +1 -0
  127. data/spec/integration/sort/sort_1.stdout +9 -0
  128. data/spec/integration/sort/sort_2.cmd +1 -0
  129. data/spec/integration/sort/sort_2.stdout +9 -0
  130. data/spec/integration/sort/sort_3.cmd +1 -0
  131. data/spec/integration/sort/sort_3.stdout +9 -0
  132. data/spec/integration/summarize/summarize_0.cmd +1 -0
  133. data/spec/integration/summarize/summarize_0.stdout +8 -0
  134. data/spec/integration/test_shell.rb +56 -0
  135. data/spec/integration/union/union_0.cmd +1 -0
  136. data/spec/integration/union/union_0.stdout +9 -0
  137. data/spec/integration/unwrap/unwrap_0.cmd +1 -0
  138. data/spec/integration/unwrap/unwrap_0.stdout +9 -0
  139. data/spec/integration/wrap/wrap_0.cmd +1 -0
  140. data/spec/integration/wrap/wrap_0.stdout +9 -0
  141. data/spec/spec_helper.rb +21 -0
  142. data/spec/test_shell.rb +10 -0
  143. data/spec/unit/doc_manager/dynamic.md +1 -0
  144. data/spec/unit/doc_manager/example.md +1 -0
  145. data/spec/unit/doc_manager/example_1.txt +11 -0
  146. data/spec/unit/doc_manager/static.md +1 -0
  147. data/spec/unit/doc_manager/test_call.rb +43 -0
  148. data/spec/unit/ext/signature/test_argv2args.rb +82 -0
  149. data/spec/unit/ext/signature/test_to_shell.rb +103 -0
  150. data/spec/unit/from_argv/test_to_attr_list.rb +30 -0
  151. data/spec/unit/from_argv/test_to_attr_name.rb +27 -0
  152. data/spec/unit/from_argv/test_to_boolean.rb +32 -0
  153. data/spec/unit/from_argv/test_to_heading.rb +37 -0
  154. data/spec/unit/from_argv/test_to_ordering.rb +28 -0
  155. data/spec/unit/from_argv/test_to_predicate.rb +26 -0
  156. data/spec/unit/from_argv/test_to_renaming.rb +23 -0
  157. data/spec/unit/from_argv/test_to_size.rb +32 -0
  158. data/spec/unit/from_argv/test_to_summarization.rb +19 -0
  159. data/spec/unit/from_argv/test_to_tuple_computation.rb +18 -0
  160. data/spec/unit/from_argv/test_to_tuple_expression.rb +38 -0
  161. data/spec/unit/main/test_class_methods.rb +44 -0
  162. data/spec/unit/operator/test_autonum.rb +28 -0
  163. data/spec/unit/operator/test_clip.rb +29 -0
  164. data/spec/unit/operator/test_coerce.rb +22 -0
  165. data/spec/unit/operator/test_compact.rb +16 -0
  166. data/spec/unit/operator/test_defaults.rb +29 -0
  167. data/spec/unit/operator/test_extend.rb +21 -0
  168. data/spec/unit/operator/test_generator.rb +37 -0
  169. data/spec/unit/operator/test_group.rb +32 -0
  170. data/spec/unit/operator/test_infer_heading.rb +16 -0
  171. data/spec/unit/operator/test_intersect.rb +18 -0
  172. data/spec/unit/operator/test_join.rb +18 -0
  173. data/spec/unit/operator/test_matching.rb +18 -0
  174. data/spec/unit/operator/test_minus.rb +18 -0
  175. data/spec/unit/operator/test_not_matching.rb +18 -0
  176. data/spec/unit/operator/test_project.rb +38 -0
  177. data/spec/unit/operator/test_quota.rb +23 -0
  178. data/spec/unit/operator/test_rank.rb +30 -0
  179. data/spec/unit/operator/test_rename.rb +21 -0
  180. data/spec/unit/operator/test_restrict.rb +36 -0
  181. data/spec/unit/operator/test_sort.rb +49 -0
  182. data/spec/unit/operator/test_summarize.rb +30 -0
  183. data/spec/unit/operator/test_ungroup.rb +28 -0
  184. data/spec/unit/operator/test_union.rb +18 -0
  185. data/spec/unit/operator/test_unwrap.rb +28 -0
  186. data/spec/unit/operator/test_wrap.rb +30 -0
  187. data/tasks/gem.rake +8 -0
  188. data/tasks/test.rake +6 -0
  189. metadata +317 -0
@@ -0,0 +1,4 @@
1
+ # 0.13.0 / 2013-07-29
2
+
3
+ The 0.13.0 version is a new birthday for alf. See `alf` main for CHANGELOG
4
+ history.
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source 'http://rubygems.org'
2
+
3
+ group :runtime do
4
+ #gem "alf-core", :git => "git://github.com/alf-tool/alf-core.git"
5
+ #gem "alf-core", :path => "../alf-core"
6
+ gem "alf-core", "~> 0.13.0"
7
+ gem "quickl", "~> 0.4.3"
8
+ end
9
+
10
+ group :development do
11
+ gem "rake", "~> 10.1"
12
+ gem "rspec", "~> 2.14"
13
+ end
14
+
15
+ group :extra do
16
+ gem "highline", "~> 1.6"
17
+ end
@@ -0,0 +1,35 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ alf-core (0.13.0)
5
+ domain (~> 1.0)
6
+ myrrha (~> 3.0)
7
+ path (~> 1.3)
8
+ sexpr (~> 0.5.1)
9
+ diff-lcs (1.2.4)
10
+ domain (1.0.0)
11
+ highline (1.6.19)
12
+ myrrha (3.0.0)
13
+ domain (~> 1.0)
14
+ path (1.3.1)
15
+ quickl (0.4.3)
16
+ rake (10.1.0)
17
+ rspec (2.14.1)
18
+ rspec-core (~> 2.14.0)
19
+ rspec-expectations (~> 2.14.0)
20
+ rspec-mocks (~> 2.14.0)
21
+ rspec-core (2.14.4)
22
+ rspec-expectations (2.14.0)
23
+ diff-lcs (>= 1.1.3, < 2.0)
24
+ rspec-mocks (2.14.1)
25
+ sexpr (0.5.1)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ alf-core (~> 0.13.0)
32
+ highline (~> 1.6)
33
+ quickl (~> 0.4.3)
34
+ rake (~> 10.1)
35
+ rspec (~> 2.14)
@@ -0,0 +1,22 @@
1
+ # The MIT Licence
2
+
3
+ Copyright (c) 2013 - Bernard Lambeau
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,13 @@
1
+ shell.gemspec
2
+ shell.noespec
3
+ CHANGELOG.md
4
+ Gemfile
5
+ Gemfile.lock
6
+ bin/**/*
7
+ lib/**/*
8
+ LICENCE.md
9
+ Manifest.txt
10
+ Rakefile
11
+ README.md
12
+ spec/**/*
13
+ tasks/**/*
@@ -0,0 +1,11 @@
1
+ # Alf::Shell
2
+
3
+ [![Build Status](https://secure.travis-ci.org/blambeau/alf-shell.png)](http://travis-ci.org/blambeau/alf-shell)
4
+ [![Dependency Status](https://gemnasium.com/blambeau/alf-shell.png)](https://gemnasium.com/blambeau/alf-shell)
5
+
6
+ Bring Alf's relational algebra to the shell
7
+
8
+ ## Links
9
+
10
+ http://github.com/blambeau/alf-shell
11
+ http://github.com/blambeau/alf
@@ -0,0 +1,11 @@
1
+ # We run tests by default
2
+ task :default => :test
3
+
4
+ #
5
+ # Install all tasks found in tasks folder
6
+ #
7
+ # See .rake files there for complete documentation.
8
+ #
9
+ Dir["tasks/*.rake"].each do |taskfile|
10
+ load taskfile
11
+ end
data/bin/alf ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ module AlfLauncher
3
+
4
+ def self.load
5
+ require "alf-shell"
6
+ end
7
+
8
+ def self.start(argv)
9
+ load
10
+ if ENV["ALF_OPTS"]
11
+ argv = Quickl.parse_commandline_args(ENV["ALF_OPTS"]) + argv
12
+ end
13
+ Alf::Shell::Main.run(argv, __FILE__)
14
+ end
15
+
16
+ end # module AlfLaucher
17
+ AlfLauncher.start(ARGV)
@@ -0,0 +1 @@
1
+ require_relative "alf/shell"
@@ -0,0 +1,48 @@
1
+ require_relative 'shell/version'
2
+ require_relative 'shell/loader'
3
+ require_relative 'shell/ext/signature'
4
+ require_relative 'shell/doc_manager'
5
+ module Alf
6
+ module Shell
7
+
8
+ # This is the main documentation extractor
9
+ DOC_EXTRACTOR = DocManager.new
10
+
11
+ # Delegator command factory
12
+ def self.Delegator()
13
+ Quickl::Delegator(){|builder|
14
+ builder.doc_extractor = DOC_EXTRACTOR
15
+ builder.class_module Command::ClassMethods
16
+ yield(builder) if block_given?
17
+ }
18
+ end
19
+
20
+ # Command factory
21
+ def self.Command()
22
+ Quickl::Command(){|builder|
23
+ builder.command_parent = Alf::Shell::Main
24
+ builder.doc_extractor = DOC_EXTRACTOR
25
+ builder.class_module Command::ClassMethods
26
+ builder.instance_module Shell::Support
27
+ yield(builder) if block_given?
28
+ }
29
+ end
30
+
31
+ # Operator factory
32
+ def self.Operator()
33
+ Quickl::Command(){|builder|
34
+ builder.command_parent = Alf::Shell::Main
35
+ builder.doc_extractor = DOC_EXTRACTOR
36
+ builder.class_module Operator::ClassMethods
37
+ builder.instance_module Shell::Support
38
+ builder.instance_module Operator::InstanceMethods
39
+ yield(builder) if block_given?
40
+ }
41
+ end
42
+
43
+ end # module Shell
44
+ end # module Alf
45
+ require_relative 'shell/from_argv'
46
+ require_relative 'shell/support'
47
+ require_relative 'shell/command'
48
+ require_relative 'shell/operator'
@@ -0,0 +1,21 @@
1
+ module Alf
2
+ module Command
3
+ module ClassMethods
4
+
5
+ # @return true
6
+ def command?
7
+ true
8
+ end
9
+
10
+ # @return false
11
+ def operator?
12
+ false
13
+ end
14
+
15
+ end # module ClassMethods
16
+ end # module Command
17
+ end # module Alf
18
+ require_relative 'command/main'
19
+ require_relative 'command/exec'
20
+ require_relative 'command/help'
21
+ require_relative 'command/show'
@@ -0,0 +1,16 @@
1
+ module Alf
2
+ module Shell
3
+ class Exec < Shell::Command()
4
+
5
+ def execute(args)
6
+ cmd = if f = args.first
7
+ Path(f).read
8
+ else
9
+ $stdin.read
10
+ end
11
+ database.query(cmd)
12
+ end
13
+
14
+ end # class Exec
15
+ end # module Shell
16
+ end # module Alf
@@ -0,0 +1,30 @@
1
+ module Alf
2
+ module Shell
3
+ class Help < Shell::Command()
4
+
5
+ # Let NoSuchCommandError be passed to higher stage
6
+ no_react_to Quickl::NoSuchCommand
7
+
8
+ options do |opt|
9
+ @dialect = :shell
10
+ opt.on('--lispy',
11
+ 'Display operator signatures in lispy DSL') do
12
+ @dialect = :lispy
13
+ end
14
+ opt.on('--shell',
15
+ 'Display operator signatures in shell DSL') do
16
+ @dialect = :shell
17
+ end
18
+ end
19
+
20
+ # Command execution
21
+ def execute(args)
22
+ sup = Quickl.super_command(self)
23
+ sub = (args.size != 1) ? sup : Quickl.sub_command!(sup, args.first)
24
+ doc = sub.documentation(:method => @dialect)
25
+ puts doc
26
+ end
27
+
28
+ end # class Help
29
+ end # module Shell
30
+ end # module Alf
@@ -0,0 +1,175 @@
1
+ module Alf
2
+ module Shell
3
+ class Main < Shell::Delegator()
4
+
5
+ class << self
6
+
7
+ def relational_operators(sort_by_name = true)
8
+ ops = subcommands.select{|cmd|
9
+ cmd.operator? and cmd.relational? and !cmd.experimental?
10
+ }
11
+ sort_operators(ops, sort_by_name)
12
+ end
13
+
14
+ def experimental_operators(sort_by_name = true)
15
+ ops = subcommands.select{|cmd|
16
+ cmd.operator? and cmd.relational? and cmd.experimental?
17
+ }
18
+ sort_operators(ops, sort_by_name)
19
+ end
20
+
21
+ def non_relational_operators(sort_by_name = true)
22
+ ops = subcommands.select{|cmd|
23
+ cmd.operator? and !cmd.relational?
24
+ }
25
+ sort_operators(ops, sort_by_name)
26
+ end
27
+
28
+ def other_non_relational_commands(sort_by_name = true)
29
+ ops = subcommands.select{|cmd|
30
+ cmd.command?
31
+ }
32
+ sort_operators(ops, sort_by_name)
33
+ end
34
+
35
+ private
36
+
37
+ def sort_operators(ops, sort_by_name)
38
+ sort_by_name ? ops.sort{|op1,op2|
39
+ op1.command_name.to_s <=> op2.command_name.to_s
40
+ } : ops
41
+ end
42
+
43
+ end # class << self
44
+
45
+ # Connection instance to use to get base relations
46
+ attr_accessor :database
47
+
48
+ # The reader to use when stdin is used as operand
49
+ attr_accessor :stdin_operand
50
+
51
+ # Output renderer
52
+ attr_accessor :renderer_class
53
+
54
+ # Rendering options
55
+ attr_reader :rendering_options
56
+
57
+ # Creates a command instance
58
+ def initialize(db = Alf.connect(Path.pwd))
59
+ @database = db
60
+ @rendering_options = {}
61
+ end
62
+
63
+ # Install options
64
+ options do |opt|
65
+ @execute = false
66
+ opt.on("-e", "--execute", "Execute one line of script (Lispy API)") do
67
+ @execute = true
68
+ end
69
+
70
+ @renderer_class = nil
71
+ Renderer.each do |name,descr,clazz|
72
+ opt.on("--#{name}", "Render output #{descr}"){
73
+ @renderer_class = clazz
74
+ }
75
+ end
76
+
77
+ opt.on('--examples', "Use the example database for database") do
78
+ @database = Alf.examples
79
+ end
80
+
81
+ opt.on('--db=DB',
82
+ "Set the database to use") do |value|
83
+ @database = Alf.connect(value)
84
+ end
85
+
86
+ @input_reader = :rash
87
+ readers = Reader.all.map{|r| r.first}
88
+ opt.on('--input-reader=READER', readers,
89
+ "Specify the kind of reader when reading on $stdin "\
90
+ "(#{readers.join(',')})") do |value|
91
+ @input_reader = value.to_sym
92
+ end
93
+
94
+ opt.on("-Idirectory",
95
+ "Specify $LOAD_PATH directory (may be used more than once)") do |val|
96
+ $LOAD_PATH.unshift val
97
+ end
98
+
99
+ opt.on('-rlibrary',
100
+ "Require the library, before executing alf") do |value|
101
+ require(value)
102
+ end
103
+
104
+ opt.on("--[no-]pretty",
105
+ "Enable/disable pretty print best effort") do |val|
106
+ self.pretty = val
107
+ end
108
+
109
+ opt.on_tail('-h', "--help", "Show help") do
110
+ raise Quickl::Help
111
+ end
112
+
113
+ opt.on_tail('-v', "--version", "Show version") do
114
+ raise Quickl::Exit, "alf #{Alf::Core::VERSION}"\
115
+ " (c) 2011-2013, Bernard Lambeau"
116
+ end
117
+ end # Alf's options
118
+
119
+ def stdin_operand
120
+ @stdin_operand || Reader.send(@input_reader, $stdin)
121
+ end
122
+
123
+ def execute(argv)
124
+ # special case where a .alf file is provided
125
+ if argv.empty? or (argv.size == 1 && Path(argv.first).exist?)
126
+ argv.unshift("exec")
127
+ end
128
+
129
+ # compile the operator, render and returns it
130
+ compile(argv){ super }.tap do |op|
131
+ render(op.to_cog) if op && requester
132
+ end
133
+ end
134
+
135
+ def pretty=(val)
136
+ @rendering_options[:pretty] = val
137
+ if val && (hl = highline)
138
+ @rendering_options[:trim_at] = hl.output_cols
139
+ @rendering_options[:page_at] = hl.output_rows
140
+ end
141
+ val
142
+ end
143
+
144
+ private
145
+
146
+ def compile(argv)
147
+ if @execute
148
+ database.query(argv.first)
149
+ else
150
+ op = yield
151
+ op = op.bind(database) if op
152
+ op
153
+ end
154
+ end
155
+
156
+ def render(operator, out = $stdout)
157
+ renderer_class = self.renderer_class || default_renderer_class
158
+ renderer = renderer_class.new(operator, rendering_options)
159
+ renderer.execute(out)
160
+ end
161
+
162
+ def highline
163
+ require 'highline'
164
+ HighLine.new($stdin, $stdout)
165
+ rescue LoadError => ex
166
+ nil
167
+ end
168
+
169
+ def default_renderer_class
170
+ $stdout.tty? ? Renderer::Text : Renderer::Rash
171
+ end
172
+
173
+ end # class Main
174
+ end # module Shell
175
+ end # module Alf
@@ -0,0 +1,49 @@
1
+ module Alf
2
+ module Shell
3
+ class Show < Shell::Command()
4
+
5
+ options do |opt|
6
+ @renderer_class = nil
7
+ Renderer.each do |name,descr,clazz|
8
+ opt.on("--#{name}", "Render output #{descr}"){
9
+ @renderer_class = clazz
10
+ }
11
+ end
12
+
13
+ @pretty = nil
14
+ opt.on("--[no-]pretty",
15
+ "Enable/disable pretty print best effort") do |val|
16
+ @pretty = val
17
+ end
18
+
19
+ @ff = nil
20
+ opt.on("--ff=FORMAT",
21
+ "Specify the floating point format") do |val|
22
+ @ff = val
23
+ end
24
+ end
25
+
26
+ def run(argv, requester)
27
+ # set requester and parse options
28
+ @requester = requester
29
+ argv = parse_options(argv, :split)
30
+
31
+ # Set options on the requester
32
+ requester.pretty = @pretty unless @pretty.nil?
33
+ requester.rendering_options[:float_format] = @ff unless @ff.nil?
34
+ requester.renderer_class = (@renderer_class || requester.renderer_class)
35
+
36
+ compile(argv)
37
+ end
38
+
39
+ def compile(argv)
40
+ operand = operands(argv.shift, 1).last
41
+ unless argv.empty?
42
+ operand = Algebra::Sort.new([operand], Shell.from_argv(argv.first, Ordering))
43
+ end
44
+ operand
45
+ end
46
+
47
+ end # class Show
48
+ end # module Shell
49
+ end # module Alf