rexe 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +7 -0
  3. data/README.md +45 -13
  4. data/exe/rexe +41 -14
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cfda8c2ee84b6747e85e1ba54954fefab0da7e246916808994c67976b291e714
4
- data.tar.gz: c2221ed77248835efaf56f0f10ffe31803eca929760f67b0c274ac2ed4c1d423
3
+ metadata.gz: 0347c7ba27865d64bda5d52cbf087f562d03dc4aad20b1ad1560c77c3611e6ad
4
+ data.tar.gz: c1ca00237ac7bd2266d13c7802099978669619a758426374967530d004b15e43
5
5
  SHA512:
6
- metadata.gz: 4b6809b8e4dc6b66cca019993eed4cfcddb65a80c75abcfccb9986206f8f59c1e2d3f4c128bff587899a24c32aeb13a082b5b597ada934b785390f9322ae46c7
7
- data.tar.gz: 5127aa52ded297c3a53529b23174f6ec1fa397c59033fd4da5bca0adc700eccd12327c84abab9e644171384a1cabf653a523bbcee8dd96b8ea4abdf17a049243
6
+ metadata.gz: c786623473eaed89cb5b72033b727bbac7dae299286b5fe685a2e16762e04a5e5d5d936bd9b98be96e84d4de1e168a64a1b1d7b054c6a92557a7671cbc14d31e
7
+ data.tar.gz: 6bd1bad2eaee29545f26e5cef1de3c7c5204723af7d0541792cdbf11806fed8fe55b845a878cdf53c44ebe19cb00026a8c9825b6e5385fefc9a776dead735a51
data/CHANGELOG.md CHANGED
@@ -1,6 +1,13 @@
1
1
  ## rexe -- Ruby Command Line Executor
2
2
 
3
3
 
4
+ ### v0.4.0
5
+
6
+ * Add -u option for loading files at current directory or above.
7
+ * Fix command line option handling for disabling verbose mode previously enabled.
8
+ * Improve README.
9
+
10
+
4
11
  ### v0.3.1
5
12
 
6
13
  * Help text fixes.
data/README.md CHANGED
@@ -26,30 +26,28 @@ rexe is a _filter_ in that it can consume standard input and emit standard outpu
26
26
  As a summary, here is the help text printed out by the application:
27
27
 
28
28
  ```
29
-
30
-
31
- rexe -- Ruby Command Line Filter -- v1.1.0 -- https://github.com/keithrbennett/rexe
29
+ exe -- Ruby Command Line Filter -- v0.3.1 -- https://github.com/keithrbennett/rexe
32
30
 
33
31
  Optionally takes standard input and runs the specified code on it, sending the result to standard output.
34
32
 
35
33
  Options:
36
34
 
37
- -h, --help Print help and exit
38
- -l, --load A_RUBY_FILE Load this Ruby source code file
39
- -m, --mode MODE Mode with which to handle input (i.e. what `self` will be in the code):
35
+ -h, --help Print help and exit
36
+ -l, --load RUBY_FILE(S) Ruby file(s) to load, comma separated
37
+ -u, --load-up RUBY_FILE(S) Ruby file(s) to load, searching up tree, comma separated
38
+ -m, --mode MODE Mode with which to handle input (i.e. what `self` will be in the code):
40
39
  -ms for each line to be handled separately as a string (default)
41
40
  -me for an enumerator of lines (least memory consumption for big data)
42
41
  -mb for 1 big string (all lines combined into single multiline string)
43
- -mn to execute the specified Ruby code on no input at all
42
+ -mn to execute the specified Ruby code on no input at all
44
43
  -r, --require REQUIRES Gems and built-in libraries (e.g. shellwords, yaml) to require, comma separated
45
- -v, --[no-]verbose Verbose mode, writes to stderr
44
+ -v, --[no-]verbose Verbose mode (logs to stderr) Verbose off short options: -v n, -v false
46
45
 
47
- If there is an .rexerc file in your home directory, it will be run as Ruby code
46
+ If there is an .rexerc file in your home directory, it will be run as Ruby code
48
47
  before processing the input.
49
48
 
50
49
  If there is an REXE_OPTIONS environment variable, its content will be prepended to the command line
51
50
  so that you can specify options implicitly (e.g. `export REXE_OPTIONS="-r awesome_print,yaml"`)
52
-
53
51
  ```
54
52
 
55
53
  ### Input Mode
@@ -73,6 +71,13 @@ Other Ruby files can be loaded by `rexe`, to, for example, define classes and me
73
71
 
74
72
  If there is a file named `.rexerc` in the home directory, that will always be loaded without explicitly requesting it on the command line.
75
73
 
74
+ #### Searching for Load Files in the Current Directory and Above
75
+
76
+ Using the `-u` option, you can specify that the file you want to load should be searched for at the specified directory _and above_. This can be handy when you have project specific configuration and you may be multiple levels below the project root.
77
+
78
+ For example, if you run `rexe` from `~/my_project/a/b/c`, and specify `-u load-me.rb`, and `load-me.rb` is located in `~/my_project`, then it will be loaded from there.
79
+
80
+ If the file is not found, the failure will be silent and the program will proceed. (Preferred behavior could go either way on this, but the current approach enables you to unconditionally specify a `-u` file in your default `REXE_OPTIONS` environment variable value and have it work regardless of the presence or absence of the file.) If verbose mode is enabled, a message about the failure to find the file will be displayed.
76
81
 
77
82
  ### Verbose Mode
78
83
 
@@ -81,17 +86,44 @@ Verbose mode outputs information to standard error (stderr). This information ca
81
86
  Here is an example of some text that might be output in verbose mode:
82
87
 
83
88
  ```
84
- rexe version 1.1.0 -- 2019-02-04 21:50:34 +0700
85
- Source Code: sort.to_a.first(3).ai
86
- Requiring awesome_print
89
+ ➜ ~  rexe -r yaml -mn -v "%w(foo bar baz).to_yaml"
90
+ rexe version 0.3.0 -- 2019-02-08 02:21:27 +0700
91
+ Source Code: %w(foo bar baz).to_yaml
92
+ Options: {:input_mode=>:no_input, :loads=>[], :requires=>["yaml"], :verbose=>true}
87
93
  Loading global config file /Users/kbennett/.rexerc
94
+ ---
95
+ - foo
96
+ - bar
97
+ - baz
98
+ rexe time elapsed: 0.03874 seconds.
88
99
  ```
89
100
 
101
+ To set verbose mode _off_ on the command line, the option parser accepts the long option `--no-verbose`, of course, but you could use instead `-v false` or `-v n`. If you should need to pass a value to `-v` to turn verbose mode _on_, then you can use `-v y` or `-v true`.
102
+
90
103
  ### The REXE_OPTIONS Environment Variable
91
104
 
92
105
  Very often you will want to call `rexe` several times with similar options. Instead of having to clutter the command line each time with these options, you can put them in an environment variable named `REXE_OPTIONS`, and they will be prepended automatically. Since they will be processed before the options on the command line, they are of lower precedence and can be overridden.
93
106
 
94
107
 
108
+ ### The ~/.rexerc Configuation File
109
+
110
+ The `.rexerc` file in your home directory (if it exists) is loaded unconditionally. Here is a place to put things that you will _always_ want. You can include commonly needed requires, but be careful because the impact on startup time may be more than you want. You can test this using verbose mode, which outputs the execution time after completing.
111
+
112
+
113
+ ### Directory-Specific Configuration Files
114
+
115
+ Although there is no built-in feature for directory-specific configuration files, this can be easily accomplished by inserting something like this in your global configuration file (~/.rexerc):
116
+
117
+ ```
118
+ dir_specific_config_file = './rexe.rc'
119
+ load dir_specific_config_file' if File.exist?(dir_specific_config_file)
120
+ ```
121
+
122
+ You should probably put this at the very end of this global configuration file so that the directory specific file will always be able to override global settings.
123
+
124
+ Note that the directory specific filename used differs from the global config filename. This is a) so that there is no recursive loading of the file when in the home directory, and b) because it is more convenient for these files that they not be hidden.
125
+
126
+
95
127
  ## Troubleshooting
96
128
 
97
129
  One common problem relates to the shell's special handling of characters. Remember that the shell will process special characters, thereby changing your text before passing it on to the Ruby code. It is good to get in the habit of putting your source code in double quotes; and if the source code itself uses quotes, use `q{}` or `Q{}` instead. For example:
data/exe/rexe CHANGED
@@ -4,14 +4,13 @@
4
4
  #
5
5
  # Inspired by https://github.com/thisredone/rb
6
6
 
7
-
8
7
  require 'optparse'
9
8
  require 'shellwords'
10
9
 
11
- # Rexe - Ruby Executor
10
+
12
11
  class Rexe < Struct.new(:input_mode, :loads, :requires, :verbose)
13
12
 
14
- VERSION = '0.3.1'
13
+ VERSION = '0.4.0'
15
14
 
16
15
  def initialize
17
16
  self.input_mode = :string
@@ -30,15 +29,16 @@ class Rexe < Struct.new(:input_mode, :loads, :requires, :verbose)
30
29
 
31
30
  Options:
32
31
 
33
- -h, --help Print help and exit
34
- -l, --load RUBY_FILE(S) Ruby file(s) to load, comma separated
35
- -m, --mode MODE Mode with which to handle input (i.e. what `self` will be in the code):
32
+ -h, --help Print help and exit
33
+ -l, --load RUBY_FILE(S) Ruby file(s) to load, comma separated
34
+ -u, --load-up RUBY_FILE(S) Ruby file(s) to load, searching up tree, comma separated
35
+ -m, --mode MODE Mode with which to handle input (i.e. what `self` will be in the code):
36
36
  -ms for each line to be handled separately as a string (default)
37
37
  -me for an enumerator of lines (least memory consumption for big data)
38
38
  -mb for 1 big string (all lines combined into single multiline string)
39
39
  -mn to execute the specified Ruby code on no input at all
40
40
  -r, --require REQUIRES Gems and built-in libraries (e.g. shellwords, yaml) to require, comma separated
41
- -v, --[no-]verbose Verbose mode (logs to stderr)
41
+ -v, --[no-]verbose Verbose mode (logs to stderr) Verbose off short options: -v n, -v false
42
42
 
43
43
  If there is an .rexerc file in your home directory, it will be run as Ruby code
44
44
  before processing the input.
@@ -62,10 +62,6 @@ class Rexe < Struct.new(:input_mode, :loads, :requires, :verbose)
62
62
  def parse_command_line
63
63
  prepend_environment_options
64
64
 
65
- add_comma_sep_tokens_to_array = ->(string, array) do
66
- string.split(',').map(&:strip).each { |r| array << r }
67
- end
68
-
69
65
  OptionParser.new do |parser|
70
66
 
71
67
  parser.on("-h", "--help", "Show help") do |_help_requested|
@@ -74,7 +70,19 @@ class Rexe < Struct.new(:input_mode, :loads, :requires, :verbose)
74
70
  end
75
71
 
76
72
  parser.on('-l', '--load RUBY_FILE(S)', 'Ruby file(s) to load, comma separated') do |v|
77
- add_comma_sep_tokens_to_array.(v, self.loads)
73
+ v.split(',').map(&:strip).each { |r| self.loads << r }
74
+ end
75
+
76
+ parser.on('-u', '--load_up RUBY_FILE(S)', 'Ruby file(s) to load, searching up tree, comma separated') do |v|
77
+ filespecs = v.split(',').map(&:strip)
78
+ filespecs.each do |filespec|
79
+ found_file = find_file_up_tree(filespec)
80
+ if found_file
81
+ self.loads << found_file
82
+ else
83
+ log_if_verbose("Did not find load file #{filespec} in this directory or higher.")
84
+ end
85
+ end
78
86
  end
79
87
 
80
88
  parser.on('-m', '--mode MODE',
@@ -99,13 +107,32 @@ class Rexe < Struct.new(:input_mode, :loads, :requires, :verbose)
99
107
  add_comma_sep_tokens_to_array.(v, self.requires)
100
108
  end
101
109
 
102
- parser.on('-v', '--[no-]verbose', 'Verbose mode (logs to stderr)') do |v|
103
- self.verbose = v
110
+ # See https://stackoverflow.com/questions/54576873/ruby-optionparser-short-code-for-boolean-option
111
+ # for an excellent explanation of this optparse incantation.
112
+ # According to the answer, valid options are:
113
+ # -v no, -v yes, -v false, -v true, -v n, -v y, -v +, but not -v -.
114
+ parser.on('-v', '--[no-]verbose [FLAG]', TrueClass, 'Verbose mode (logs to stderr)') do |v|
115
+ self.verbose = (v.nil? ? true : v)
104
116
  end
105
117
  end.parse!
106
118
  end
107
119
 
108
120
 
121
+ # Looks in this directory and all above it for the filename
122
+ # @return the first match found, or nil if none
123
+ def find_file_up_tree(filespec)
124
+ filespec = File.expand_path(filespec)
125
+ basename = File.basename(filespec)
126
+
127
+ loop do
128
+ return filespec if File.exist?(filespec)
129
+ return nil if File.dirname(filespec) == File::SEPARATOR
130
+ new_dir = File.expand_path(File.join(File.dirname(filespec), '..'))
131
+ filespec = File.join(new_dir, basename)
132
+ end
133
+ end
134
+
135
+
109
136
  def load_global_config_if_exists
110
137
  filespec = File.join(Dir.home, '.rexerc')
111
138
  exists = File.exists?(filespec)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rexe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keith Bennett
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-02-07 00:00:00.000000000 Z
11
+ date: 2019-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler