Compare commits

...

5 Commits

  1. 1
      LICENSE
  2. 6
      Rakefile
  3. 11
      lib/main.rb
  4. 18
      lib/main/cast.rb
  5. 2
      lib/main/daemon.rb
  6. 2
      lib/main/getoptlong.rb
  7. 1
      lib/main/mode.rb
  8. 4
      lib/main/parameter.rb
  9. 15
      lib/main/program/class_methods.rb
  10. 15
      lib/main/program/instance_methods.rb
  11. 4
      lib/main/stdext.rb
  12. 8
      lib/main/test.rb
  13. 35
      lib/main/usage.rb
  14. 2
      lib/main/util.rb
  15. 16
      main.gemspec
  16. 21
      test/main_test.rb

@ -1 +0,0 @@
same as Ruby's

@ -4,7 +4,7 @@ This.email = "ara.t.howard@gmail.com"
This.homepage = "https://github.com/ahoward/#{ This.lib }"
task :license do
open('LICENSE', 'w'){|fd| fd.puts "same as ruby's"}
open('LICENSE', 'w'){|fd| fd.puts "Ruby"}
end
task :default do
@ -93,7 +93,7 @@ task :gemspec do
test_files = "test/#{ lib }.rb" if File.file?("test/#{ lib }.rb")
summary = object.respond_to?(:summary) ? object.summary : "summary: #{ lib } kicks the ass"
description = object.respond_to?(:description) ? object.description : "description: #{ lib } kicks the ass"
license = object.respond_to?(:license) ? object.license : "same as ruby's"
license = object.respond_to?(:license) ? object.license : "Ruby"
if This.extensions.nil?
This.extensions = []
@ -297,7 +297,7 @@ BEGIN {
# discover full path to this ruby executable
#
c = Config::CONFIG
c = RbConfig::CONFIG
bindir = c["bindir"] || c['BINDIR']
ruby_install_name = c['ruby_install_name'] || c['RUBY_INSTALL_NAME'] || 'ruby'
ruby_ext = c['EXEEXT'] || ''

@ -1,7 +1,7 @@
module Main
# top level constants
#
Main::VERSION = '6.2.0' unless
Main::VERSION = '6.2.3' unless
defined? Main::VERSION
def Main.version() Main::VERSION end
@ -11,7 +11,6 @@ module Main
Main::LIBDIR = File.join(File.dirname(File.expand_path(__FILE__)), self.name.downcase, '') unless
defined? Main::LIBDIR
def self.libdir() Main::LIBDIR end
Main::EXIT_SUCCESS = 0 unless defined? Main::EXIT_SUCCESS
Main::EXIT_FAILURE = 1 unless defined? Main::EXIT_FAILURE
@ -22,10 +21,10 @@ module Main
#
def Main.dependencies
{
'chronic' => [ 'chronic' , '>= 0.6.2' ] ,
'fattr' => [ 'fattr' , '>= 2.2.0' ] ,
'arrayfields' => [ 'arrayfields' , '>= 4.7.4' ] ,
'map' => [ 'map' , '>= 6.1.0' ] ,
'chronic' => [ 'chronic', '~> 0.6', '>= 0.6.2' ] ,
'fattr' => [ 'fattr', '~> 2.2', '>= 2.2.0' ] ,
'arrayfields' => [ 'arrayfields', '~> 4.7', '>= 4.7.4' ] ,
'map' => [ 'map', '~> 6.1', '>= 6.1.0' ] ,
}
end

@ -7,10 +7,15 @@ module Main
List = []
def self.cast m, &b
define_method m, &b
export m
List << m.to_s
def Cast.cast(*args, &block)
if args.first && block
method = args.shift
define_method(method, &block)
export(method)
List << (method.to_s)
else
Cast.send(*args)
end
end
cast :boolean do |obj|
@ -152,12 +157,7 @@ module Main
m
raise ArgumentError, "ambiguous cast: #{ sym.inspect } (#{ List.join ',' })" unless
candidates.empty? or m.to_s == sym.to_s
this = self
lambda{|obj| method(m).call obj}
end
def Cast.cast(which, *args, &block)
Cast.send(which, *args, &block)
end
end
end

@ -225,7 +225,7 @@ module Main
begin
cmd_stop
break
rescue Object => e
rescue Object
if alive?
sleep(rand)
else

@ -70,7 +70,7 @@ class GetoptLong
#
# Whether error messages are output to stderr.
#
@quiet = FALSE
@quiet = false
#
# Status code.

@ -20,7 +20,6 @@ module Main
end
def find_by_mode m, options = {}
quiet = options['quiet'] || options[:quiet]
each_pair do |mode, klass|
return mode if mode == m
end

@ -240,8 +240,6 @@ module Main
self
end
this = self
%w[ before instead after ].each do |which|
getter = "error_handler_#{ which }"
query = "error_handler_#{ which }?"
@ -462,7 +460,7 @@ module Main
i += 1
b = argv[idx + 1]
s = "#{ a }#{ b }"
m, key, *ignored = kre.match(s).to_a
m, key, * = kre.match(s).to_a
if m
replacements[i] ||= a.gsub %r/^\s*#{ key }/, opt
next

@ -107,7 +107,6 @@ module Main
#
def dynamically_extend_via_commandline_modes!
self.breadth_first_modes = modes.dup
size = modes.size
loop do
modes.each do |mode|
@ -183,9 +182,9 @@ module Main
undef_method 'usage'
def usage(*args, &block)
usage! unless defined? @usage
usage! unless defined? @usage
return @usage if args.empty? and block.nil?
key, value, *ignored = args
key, value, * = args
value = block.call if block
@usage[key.to_s] = value.to_s
end
@ -255,7 +254,7 @@ module Main
%w[ examples samples api ].each do |chunkname|
module_eval <<-code
def #{ chunkname } *a, &b
def #{ chunkname } *a, &b
txt = b ? b.call : a.join("\\n")
usage['#{ chunkname }'] = txt
end
@ -270,7 +269,7 @@ module Main
end
def main_env(*args, &block)
@main_env ||= main_env!
@main_env ||= main_env!
if args.empty?
@main_env
@ -301,14 +300,14 @@ module Main
end
def state_dirname(*args)
@state_dirname = File.join(*args) unless args.empty?
@state_dirname = File.join(*args) unless args.empty?
@state_dirname ||= main_env('STATE_DIRNAME')
@state_dirname ||= Util.home
@state_dirname
end
def state_basename(*args)
@state_basename = File.join(*args) unless args.empty?
@state_basename = File.join(*args) unless args.empty?
@state_basename ||= main_env('STATE_BASENAME')
@state_basename ||= ".#{ name }"
@state_basename
@ -367,7 +366,7 @@ module Main
else
[]
end
dash = lines.shift if lines.first.to_s =~ /^---/
lines.shift if lines.first.to_s =~ /^---/
require 'fileutils' unless defined?(FileUtils)
FileUtils.mkdir_p(File.dirname(config_path))
open(config_path, 'w') do |fd|

@ -1,3 +1,5 @@
require "securerandom"
module Main
class Program
module InstanceMethods
@ -15,7 +17,7 @@ module Main
fattr('params')
fattr('finalizers')
%w(
%w(
program name synopsis description author version
exit_status exit_success exit_failure exit_warn exit_warning
logger_level
@ -42,6 +44,7 @@ module Main
def pre_initialize() :hook end
def before_initialize() :hook end
def main_initialize()
@id = SecureRandom.uuid
setup_finalizers
setup_io_redirection
setup_logging
@ -53,7 +56,7 @@ module Main
def setup_finalizers
@finalizers ||= []
finalizers = @finalizers
ObjectSpace.define_finalizer(self) do
ObjectSpace.define_finalizer(@id) do
while((f = finalizers.pop)); f.call; end
end
end
@ -76,7 +79,7 @@ module Main
undef_method 'logger='
def logger= log
unless(defined?(@logger) and @logger == log)
case log
case log
when ::Logger, Logger
@logger = log
else
@ -102,7 +105,7 @@ module Main
)
end
end
undef_method 'stdin='
def stdin= io
unless(defined?(@stdin) and (@stdin == io))
@ -165,7 +168,7 @@ module Main
end
end
end
def pre_parse_parameters() :hook end
def before_parse_parameters() :hook end
def parse_parameters
@ -207,7 +210,7 @@ module Main
def shell!
Pry.hooks.clear_all
prompt = "#{ name } > "
Pry.config.prompt = proc{|*a| prompt }
Pry.config.prompt = proc{|*a| prompt }
binding.pry
end

@ -14,7 +14,7 @@ private
def abort(message = nil)
if message
message = message.to_s
message.singleton_class{ fattr 'abort' => true }
message.singleton_class{ fattr 'abort' => true } unless message.frozen?
STDERR.puts message
end
exit 1
@ -27,7 +27,7 @@ module Process
def abort(message = nil)
if message
message = message.to_s
message.singleton_class{ fattr 'abort' => true }
message.singleton_class{ fattr 'abort' => true } unless message.frozen?
STDERR.puts message
end
exit 1

@ -11,10 +11,10 @@ module Main
argv = options[:argv]
env = options[:env]
logger = options[:logger]
stdin = options[:stdin]
stdout = options[:stdout]
stderr = options[:stderr]
#logger = options[:logger]
#stdin = options[:stdin]
#stdout = options[:stdout]
#stderr = options[:stderr]
Main.push_ios!

@ -142,27 +142,26 @@ module Main
parameters = arguments + keywords + options + environment
s =
parameters.map do |p|
ps = ''
ps << Util.columnize("#{ p.synopsis }", :indent => 2, :width => 78)
#ps << Util.columnize("* #{ p.synopsis }", :indent => 2, :width => 78)
parameters.map do |p|
ps = ''
ps << Util.columnize("#{ p.synopsis }", :indent => 2, :width => 78)
#ps << Util.columnize("* #{ p.synopsis }", :indent => 2, :width => 78)
#ps << "\n"
if p.description?
ps << "\n"
ps << Util.columnize("#{ p.description }", :indent => 6, :width => 78)
#ps << Util.columnize(p.description, :indent => 6, :width => 78)
#ps << "\n"
if p.description?
end
#ps << "\n"
unless(p.examples.nil? or p.examples.empty?)
p.examples.each do |example|
ps << "\n"
ps << Util.columnize("#{ p.description }", :indent => 6, :width => 78)
#ps << Util.columnize(p.description, :indent => 6, :width => 78)
#ps << "\n"
ps << Util.columnize("#{ example }", :indent => 8, :width => 78)
end
#ps << "\n"
unless(p.examples.nil? or p.examples.empty?)
p.examples.each do |example|
ps << "\n"
ps << Util.columnize("#{ example }", :indent => 8, :width => 78)
end
end
ps
end.join("\n")
end
ps
end.join("\n")
end
def author_section

@ -44,7 +44,7 @@ module Main
def columnize buf, opts = {}
width = Util.getopt 'width', opts, 80
indent = Util.getopt 'indent', opts
indent = Fixnum === indent ? (' ' * indent) : "#{ indent }"
indent = Numeric === indent ? (' ' * indent.to_i) : "#{ indent }"
column = []
words = buf.split %r/\s+/o
row = "#{ indent }"

@ -3,19 +3,17 @@
Gem::Specification::new do |spec|
spec.name = "main"
spec.version = "6.2.0"
spec.version = "6.2.3"
spec.platform = Gem::Platform::RUBY
spec.summary = "main"
spec.description = "a class factory and dsl for generating command line programs real quick"
spec.license = "same as ruby's"
spec.license = "Ruby"
spec.files =
["LICENSE",
"README",
["README",
"README.erb",
"Rakefile",
"TODO",
"a.rb",
"lib",
"lib/main",
"lib/main.rb",
@ -57,13 +55,13 @@ Gem::Specification::new do |spec|
spec.test_files = nil
spec.add_dependency(*["chronic", ">= 0.6.2"])
spec.add_dependency(*["chronic", "~> 0.6", ">= 0.6.2"])
spec.add_dependency(*["fattr", ">= 2.2.0"])
spec.add_dependency(*["fattr", "~> 2.2", ">= 2.2.0"])
spec.add_dependency(*["arrayfields", ">= 4.7.4"])
spec.add_dependency(*["arrayfields", "~> 4.7", ">= 4.7.4"])
spec.add_dependency(*["map", ">= 6.1.0"])
spec.add_dependency(*["map", "~> 6.1", ">= 6.1.0"])
spec.extensions.push(*[])

@ -117,7 +117,6 @@ class T < Test::Unit::TestCase
# parameter parsing
#
def test_0080
p = nil
assert_raises(Main::Parameter::NotGiven){
main(){
argument 'foo'
@ -129,7 +128,6 @@ class T < Test::Unit::TestCase
p = nil
m = nil
argv = %w[ 42 ]
given = nil
assert_nothing_raised{
main(argv.dup){
argument 'foo'
@ -144,7 +142,6 @@ class T < Test::Unit::TestCase
def test_0100
p = nil
argv = %w[]
given = nil
assert_nothing_raised{
main(argv){
p = argument('foo'){ optional }
@ -160,7 +157,6 @@ class T < Test::Unit::TestCase
def test_0101
p = nil
argv = %w[]
given = nil
assert_nothing_raised{
main(argv){
p = argument('foo'){ required false }
@ -492,12 +488,12 @@ class T < Test::Unit::TestCase
#
def test_0280
assert_nothing_raised{
u = Main::Usage.new
Main::Usage.new
}
end
def test_0290
assert_nothing_raised{
u = Main::Usage.default(Main.factory)
Main::Usage.default(Main.factory)
}
end
def test_0300
@ -733,32 +729,29 @@ class T < Test::Unit::TestCase
assert m.param['zero_or_more'].values == argv
end
def test_0450
m = nil
argv = %w()
assert_raises(Main::Parameter::NotGiven){
main(argv.dup) {
argument('one_or_more'){ arity(-2) }
run{ m = self }
run{ self }
}
}
end
def test_0460
m = nil
argv = %w( a )
assert_raises(Main::Parameter::Arity){
main(argv.dup) {
argument('two_or_more'){ arity(-3) }
run{ m = self }
run{ self }
}
}
end
def test_0470
m = nil
argv = %w( a )
assert_raises(Main::Parameter::Arity){
main(argv.dup) {
argument('two_or_more'){ arity(-4) }
run{ m = self }
run{ self }
}
}
end
@ -830,12 +823,11 @@ class T < Test::Unit::TestCase
#
def test_0500
name = 'arity_zero_paramter_attr'
m = nil
argv = %w( )
assert_raises(Main::Parameter::Arity){
main(argv.dup) {
argument(name){ arity 0 }
run{ m = self }
run{ self }
}
}
end
@ -897,7 +889,6 @@ class T < Test::Unit::TestCase
end
def test_0550
name = 'mode_argument_with_help_parameter_outputs_help'
p = nil
argv = %w( foo help )
assert_nothing_raised{

Loading…
Cancel
Save