file_editor_cli 0.6.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 (4) hide show
  1. data/README.txt +45 -0
  2. data/bin/editfile +106 -0
  3. data/file_editor_cli.gemspec +17 -0
  4. metadata +68 -0
data/README.txt ADDED
@@ -0,0 +1,45 @@
1
+ File Editor Command Line Interface makes it easy to edit one or more files in place from the command line.
2
+
3
+ ===Using File Editor Command Line Interface
4
+
5
+ From the command line, run the editfile script with one or more file names, a regular expression, a substition string, and options for ignoring case, non-global search, multiline search, and keeping a backup file. Although not always necessary, it is recommended to put your regex and substitution string in quotes.
6
+
7
+ ===Command Line Options
8
+
9
+ <tt>-h</tt>:: Help screen.
10
+ <tt>-r "regex"</tt>:: The regular expression.
11
+ <tt>-s "substitution_string"</tt>:: The substitution string.
12
+ <tt>-i</tt>:: Ignore case.
13
+ <tt>-m</tt>:: Multiline mode.
14
+ <tt>-l</tt>:: Non-global substition (equivalent of sub instead of gsub).
15
+ <tt>-b</tt>:: Saves a backup of any changed file.
16
+
17
+ ===Examples
18
+
19
+ ====Edit a file, substituting "ruby" for "java"
20
+
21
+ editfile -r "java" -s "ruby" test.txt
22
+
23
+ ====Edit two files
24
+
25
+ editfile -r "java" -s "ruby" test.txt test2.txt
26
+
27
+ ====Keep a backup file
28
+
29
+ editfile -b -r "java" -s "ruby" test.txt
30
+
31
+ ====Ignore case
32
+
33
+ editfile -i -r "java" -s "ruby" test.txt
34
+
35
+ ====Do per-line non-global substition (equivalent of sub instead of gsub)
36
+
37
+ editfile -l -r "java" -s "ruby" test.txt
38
+
39
+ ====Use multiline search (replace matches across line endings)
40
+
41
+ editfile -m -r "ja.*?va" -s "ruby" test.txt
42
+
43
+ ====Note: File Editor Command Line Interface uses the file_editor gem as its regular expression and substitution engine, so no library files are necessary.
44
+
45
+
data/bin/editfile ADDED
@@ -0,0 +1,106 @@
1
+ #!usr/bin/env ruby
2
+
3
+ ##############################################################
4
+ # Copyright (c) 2013 Mark Buechler
5
+ # All rights reserved.
6
+
7
+ # Permission is granted for use, copying, modification, distribution,
8
+ # and distribution of modified versions of this work as long as the
9
+ # above copyright notice is included.
10
+ ##############################################################
11
+
12
+ ##############################################################
13
+ #Run this executable to edit files in place
14
+ ##############################################################
15
+
16
+ require 'file_editor'
17
+ require 'optparse'
18
+
19
+ ##############################################
20
+ ## Method Definitions
21
+ ##############################################
22
+
23
+ #Parses command line options
24
+ def prepare_options()
25
+ options = {}
26
+ options[:global] = true #file editor default
27
+ OptionParser.new do |opts|
28
+ opts.banner = "usage: editfile [options] [file_paths]"
29
+ opts.on("-h", "--help", "Display help screen") do
30
+ puts opts
31
+ exit
32
+ end
33
+ opts.on("-r", "--regex REGEX", "The regex to use") do |r|
34
+ options[:regex] = r
35
+ end
36
+ opts.on("-s", "--subtitution_string SUBSTITUTION_STRING", "The substitution string") do |s|
37
+ options[:substitution_string] = s
38
+ end
39
+ opts.on("-b", "--backup", "Keep backup file") do
40
+ options[:keep_backup] = true
41
+ end
42
+ opts.on("-i", "--ignore_case", "Ignore case") do
43
+ options[:ignore_case] = true
44
+ end
45
+ opts.on("-m", "--multiline", "Multiline") do
46
+ options[:multiline] = true
47
+ end
48
+ opts.on("-l", "--non_global", "First match in a line only") do
49
+ options[:global] = false
50
+ end
51
+ end.parse!
52
+ options
53
+ end
54
+
55
+ #Validates options and file-name arguments
56
+ def validate(options, file_names)
57
+ if ( (options[:regex].nil?) || (options[:regex].start_with?('-')) )
58
+ puts "Provide a regex using the -r option"
59
+ exit 1
60
+ end
61
+ if ( (options[:substitution_string].nil?) || (options[:substitution_string].start_with?('-')) )
62
+ puts "Provide a substition string using the -s option"
63
+ exit 1
64
+ end
65
+ raise "Provide the name of at least one file to edit" unless file_names.size > 0 #file_editor gem will handle basic file validations (exists, is-file, etc)
66
+ end
67
+
68
+ def build_regex(options)
69
+ if (options[:multiline] && options[:ignore_case])
70
+ regex = Regexp.new(options[:regex], (Regexp::MULTILINE | Regexp::IGNORECASE))
71
+ elsif (options[:multiline])
72
+ regex = Regexp.new(options[:regex], Regexp::MULTILINE)
73
+ elsif (options[:ignore_case])
74
+ regex = Regexp.new(options[:regex], Regexp::IGNORECASE)
75
+ else
76
+ regex = Regexp.new(options[:regex])
77
+ end
78
+ end
79
+
80
+ def edit(options, regex, file_names)
81
+ file_names.each do |file_name|
82
+ FileEditor.edit(file_name) do |editor|
83
+ editor.regex = regex
84
+ editor.substitution_string = options[:substitution_string]
85
+ editor.keep_backup = options[:keep_backup]
86
+ editor.global = options[:global]
87
+ editor.run
88
+ end
89
+ end
90
+ end
91
+
92
+ ##############################################
93
+ ## Program
94
+ ##############################################
95
+
96
+ begin
97
+ files = ARGV
98
+ options = prepare_options
99
+ validate(options, files)
100
+ reg = build_regex(options)
101
+ edit(options, reg, files)
102
+ rescue => e
103
+ puts "#{e.message}"
104
+ exit 1
105
+ end
106
+
@@ -0,0 +1,17 @@
1
+ Gem::Specification.new do |s|
2
+ s.name="file_editor_cli"
3
+ s.version="0.6.0"
4
+ s.platform=Gem::Platform::RUBY
5
+ s.date="2013-02-25"
6
+ s.summary="Command line utility to edit one or more files in place"
7
+ s.description="File editor command line makes it easy to edit files in place, with options for saving a backup"
8
+ s.author="Mark Buechler"
9
+ s.files=Dir["file_editor_cli.gemspec", "README.txt", "lib/**/*"]
10
+ s.homepage="http://rubygems.org/file_editor_cli"
11
+ s.executables=["editfile"]
12
+ s.require_paths=["lib"]
13
+ s.add_runtime_dependency "file_editor"
14
+ s.has_rdoc=true
15
+ s.extra_rdoc_files=["README.txt"]
16
+ s.rdoc_options=["--main", "README.txt"]
17
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: file_editor_cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mark Buechler
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: file_editor
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: File editor command line makes it easy to edit files in place, with options
31
+ for saving a backup
32
+ email:
33
+ executables:
34
+ - editfile
35
+ extensions: []
36
+ extra_rdoc_files:
37
+ - README.txt
38
+ files:
39
+ - file_editor_cli.gemspec
40
+ - README.txt
41
+ - bin/editfile
42
+ homepage: http://rubygems.org/file_editor_cli
43
+ licenses: []
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --main
47
+ - README.txt
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 1.8.24
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Command line utility to edit one or more files in place
68
+ test_files: []