mysh 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +95 -28
  3. data/lib/mysh.rb +13 -39
  4. data/lib/mysh/binding_wrapper.rb +21 -0
  5. data/lib/mysh/exceptions.rb +4 -0
  6. data/lib/mysh/expression.rb +11 -4
  7. data/lib/mysh/external_ruby.rb +20 -7
  8. data/lib/mysh/globalize.rb +18 -0
  9. data/lib/mysh/handlebars.rb +0 -3
  10. data/lib/mysh/init.rb +26 -0
  11. data/lib/mysh/internal/action.rb +7 -2
  12. data/lib/mysh/internal/action_pool.rb +9 -4
  13. data/lib/mysh/internal/actions/command_line.rb +56 -0
  14. data/lib/mysh/internal/actions/command_line/debug.rb +39 -0
  15. data/lib/mysh/internal/actions/command_line/init.rb +50 -0
  16. data/lib/mysh/internal/actions/command_line/load.rb +25 -0
  17. data/lib/mysh/internal/actions/command_line/post_prompt.rb +44 -0
  18. data/lib/mysh/internal/actions/command_line/pre_prompt.rb +44 -0
  19. data/lib/mysh/internal/actions/command_line/prompt.rb +42 -0
  20. data/lib/mysh/internal/actions/command_line/quit.rb +19 -0
  21. data/lib/mysh/internal/actions/command_line/usage.rb +23 -0
  22. data/lib/mysh/internal/actions/comment.rb +20 -0
  23. data/lib/mysh/internal/actions/elapsed.rb +27 -0
  24. data/lib/mysh/internal/actions/exit.rb +2 -3
  25. data/lib/mysh/internal/actions/gls.rb +1 -1
  26. data/lib/mysh/internal/actions/help/expr.txt +6 -9
  27. data/lib/mysh/internal/actions/help/quick.txt +6 -0
  28. data/lib/mysh/internal/actions/help/sub_help.rb +1 -0
  29. data/lib/mysh/internal/actions/help/usage.txt +8 -0
  30. data/lib/mysh/internal/actions/help/vars.txt +2 -2
  31. data/lib/mysh/internal/actions/load.rb +42 -0
  32. data/lib/mysh/internal/actions/quit.rb +18 -0
  33. data/lib/mysh/internal/actions/say.rb +20 -0
  34. data/lib/mysh/internal/actions/show/env.rb +13 -11
  35. data/lib/mysh/internal/actions/type.rb +4 -3
  36. data/lib/mysh/internal/actions/vars.rb +5 -5
  37. data/lib/mysh/internal/manage.rb +1 -1
  38. data/lib/mysh/process.rb +52 -0
  39. data/lib/mysh/quick.rb +3 -1
  40. data/lib/mysh/shell_variables.rb +0 -1
  41. data/lib/mysh/sources/console.rb +44 -0
  42. data/lib/mysh/{user_input → sources}/parse.rb +1 -1
  43. data/lib/mysh/{user_input/smart_source.rb → sources/smart_auto_complete.rb} +1 -1
  44. data/lib/mysh/sources/string.rb +33 -0
  45. data/lib/mysh/user_input.rb +14 -30
  46. data/lib/mysh/version.rb +1 -1
  47. data/samples/load.rb +6 -0
  48. data/samples/script.mysh +11 -0
  49. data/samples/sleep.rb +6 -0
  50. data/samples/sleep.txt +2 -0
  51. data/tests/my_shell_tests.rb +6 -4
  52. metadata +30 -5
  53. data/lib/mysh/shell_variables/globalize.rb +0 -9
@@ -19,8 +19,8 @@ module Mysh
19
19
 
20
20
  #Parse the string and call the action.
21
21
  def quick_parse_and_call(str)
22
- call(Mysh.parse_args(str[1...-1]))
23
- :action
22
+ call(Mysh.parse_args(str[1..-1].chomp))
23
+ :internal
24
24
  end
25
25
 
26
26
  #Get information about the action.
@@ -33,6 +33,11 @@ module Mysh
33
33
  @exec_binding.eval(str)
34
34
  end
35
35
 
36
+ #Get the name without any argument descriptions.
37
+ def short_name
38
+ name.split[0] || ""
39
+ end
40
+
36
41
  private
37
42
 
38
43
  #Create a binding for mysh to execute expressions in.
@@ -20,15 +20,20 @@ module Mysh
20
20
  @pool[index]
21
21
  end
22
22
 
23
+ #Does this action exist?
24
+ def exists?(index)
25
+ @pool.has_key?(index)
26
+ end
27
+
23
28
  #Add an action to the pool.
24
29
  def add_action(action)
25
- split_name = action.name.split[0] || ""
30
+ short_name = action.short_name
26
31
 
27
- if @pool.key?(split_name)
28
- fail "Add error: Action #{split_name.inspect} already exists in #{pool_name}."
32
+ if @pool.key?(short_name)
33
+ fail "Add error: Action #{short_name.inspect} already exists in #{pool_name}."
29
34
  end
30
35
 
31
- @pool[split_name] = action
36
+ @pool[short_name] = action
32
37
  end
33
38
 
34
39
  #Get information on all actions.
@@ -0,0 +1,56 @@
1
+ # coding: utf-8
2
+
3
+ #* mysh/internal/actions/command_line.rb -- The mysh internal command line option.
4
+ module Mysh
5
+
6
+ # Action pool of command line options.
7
+ COMMAND_LINE = ActionPool.new("COMMAND_LINE")
8
+
9
+ #* mysh/internal/actions/command_line.rb -- The mysh internal command line option.
10
+ class CommandOption < Action
11
+
12
+ #Execute a pre-boot command line option.
13
+ def pre_boot(_args); end
14
+
15
+ #Execute a post-boot command line option.
16
+ def post_boot(_args); end
17
+
18
+ alias :call :pre_boot
19
+
20
+ #Get an argument for an option.
21
+ def get_arg(read_point)
22
+ result = read_point.next
23
+ fail if COMMAND_LINE.exists?(result)
24
+ result
25
+ rescue
26
+ fail "Error in #{short_name.inspect}: Invalid argument: #{result.inspect}"
27
+ end
28
+
29
+ end
30
+
31
+ #Execute command line options.
32
+ #<br>Endemic Code Smells
33
+ #* :reek:TooManyStatements
34
+ def self.process_command_args(args, phase)
35
+ read_point = args.each
36
+
37
+ loop do
38
+ next_option = read_point.next
39
+ next_command = COMMAND_LINE[next_option]
40
+ raise "Error: Invalid option #{next_option.inspect}" unless next_command
41
+ next_command.send(phase, read_point)
42
+ end
43
+
44
+ rescue => err
45
+ unless (msg = err.to_s).empty?
46
+ puts "", msg, ""
47
+ end
48
+
49
+ HELP["usage"].call([])
50
+ exit
51
+ end
52
+
53
+ end
54
+
55
+ #Load up the extra help actions!
56
+ Dir[Mysh::Action::ACTIONS_PATH + 'command_line/*.rb'].each {|file| require file }
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+
3
+ #* mysh/internal/actions/command_line/debug.rb -- The mysh help command debug.
4
+ module Mysh
5
+
6
+ #* mysh/internal/actions/command_line/debug.rb -- The mysh help command debug.
7
+ class DebugOption < CommandOption
8
+
9
+ #Execute the help command line option.
10
+ #<br>Endemic Code Smells
11
+ #* :reek:UtilityFunction
12
+ def post_boot(_args)
13
+ MNV[:debug] = "on"
14
+ end
15
+
16
+ end
17
+
18
+ #Add the debug command line option to the library.
19
+ desc = 'Turn on mysh debugging.'
20
+ COMMAND_LINE.add_action(DebugOption.new('--debug', desc))
21
+ COMMAND_LINE.add_action(DebugOption.new('-d', desc))
22
+
23
+ #* mysh/internal/actions/command_line/debug.rb -- The mysh help command no debug.
24
+ class NoDebugOption < CommandOption
25
+
26
+ #Execute the help command line option.
27
+ #<br>Endemic Code Smells
28
+ #* :reek:UtilityFunction
29
+ def post_boot(_args)
30
+ MNV[:debug] = "off"
31
+ end
32
+
33
+ end
34
+
35
+ #Add the debug command line option to the library.
36
+ desc = 'Turn off mysh debugging.'
37
+ COMMAND_LINE.add_action(NoDebugOption.new('--no-debug', desc))
38
+ COMMAND_LINE.add_action(NoDebugOption.new('-nd', desc))
39
+ end
@@ -0,0 +1,50 @@
1
+ # coding: utf-8
2
+
3
+ #* mysh/internal/actions/command_line/init.rb -- The mysh init and no-init commands.
4
+ module Mysh
5
+
6
+ #* mysh/internal/actions/command_line/init.rb -- The mysh init command.
7
+ class InitOption < CommandOption
8
+
9
+ #Skip over the argument for pre_boot.
10
+ def pre_boot(read_point)
11
+ file_name = get_arg(read_point).decorate
12
+
13
+ if $mysh_init_file
14
+ $mysh_init_file = $mysh_init_file.in_array + [file_name]
15
+ else
16
+ $mysh_init_file = file_name
17
+ end
18
+
19
+ mysh "load #{file_name}"
20
+ end
21
+
22
+ #Execute the init command line option.
23
+ def post_boot(read_point)
24
+ get_arg(read_point)
25
+ end
26
+
27
+ end
28
+
29
+ #Add the init command line option to the library.
30
+ desc = 'Initialize mysh by loading the specified file.'
31
+ COMMAND_LINE.add_action(InitOption.new('--init filename', desc))
32
+ COMMAND_LINE.add_action(InitOption.new('-i filename', desc))
33
+
34
+
35
+ #* mysh/internal/actions/command_line/init.rb -- The mysh no init command.
36
+ class NoInitOption < CommandOption
37
+
38
+ #Skip over the argument for pre_boot.
39
+ def pre_boot(_args)
40
+ fail "The mysh is already initialized." if $mysh_init_file
41
+ $mysh_init_file = "<none>"
42
+ end
43
+
44
+ end
45
+
46
+ #Add the no init command line option to the library.
47
+ desc = 'Do not load a file to initialize mysh.'
48
+ COMMAND_LINE.add_action(NoInitOption.new('--no-init', desc))
49
+ COMMAND_LINE.add_action(NoInitOption.new('-ni', desc))
50
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+
3
+ #* mysh/internal/actions/command_line/load.rb -- The mysh load command.
4
+ module Mysh
5
+
6
+ #* mysh/internal/actions/command_line/load.rb -- The mysh load command.
7
+ class LoadOption < CommandOption
8
+
9
+ #Skip over the argument for pre_boot.
10
+ def pre_boot(read_point)
11
+ get_arg(read_point)
12
+ end
13
+
14
+ #Execute the load command line option.
15
+ def post_boot(read_point)
16
+ mysh "load #{get_arg(read_point).decorate}"
17
+ end
18
+
19
+ end
20
+
21
+ #Add the load command line option to the library.
22
+ desc = 'Load the specified file into the mysh.'
23
+ COMMAND_LINE.add_action(LoadOption.new('--load filename', desc))
24
+ COMMAND_LINE.add_action(LoadOption.new('-l filename', desc))
25
+ end
@@ -0,0 +1,44 @@
1
+ # coding: utf-8
2
+
3
+ #* mysh/internal/actions/command_line/prompt.rb -- The mysh post prompt command.
4
+ module Mysh
5
+
6
+ #* mysh/internal/actions/command_line/post_prompt.rb -- The mysh post prompt command.
7
+ class PostpromptOption < CommandOption
8
+
9
+ #Skip over the argument for pre_boot.
10
+ def pre_boot(read_point)
11
+ get_arg(read_point)
12
+ end
13
+
14
+ #Execute the prompt command line option.
15
+ #<br>Endemic Code Smells
16
+ #* :reek:UtilityFunction
17
+ def post_boot(read_point)
18
+ MNV[:post_prompt] = get_arg(read_point)
19
+ end
20
+
21
+ end
22
+
23
+ #Add the post prompt command line option to the library.
24
+ desc = 'Set the mysh line continuation prompt to "str".'
25
+ COMMAND_LINE.add_action(PostpromptOption.new('--post-prompt "str"', desc))
26
+ COMMAND_LINE.add_action(PostpromptOption.new('-pp "str"', desc))
27
+
28
+ #* mysh/internal/actions/command_line/post_prompt.rb -- The mysh command no post prompt.
29
+ class NoPostpromptOption < CommandOption
30
+
31
+ #Execute the no post prompt command line option.
32
+ #<br>Endemic Code Smells
33
+ #* :reek:UtilityFunction
34
+ def post_boot(_args)
35
+ MNV[:post_prompt] = ""
36
+ end
37
+
38
+ end
39
+
40
+ #Add the prompt command line option to the library.
41
+ desc = 'Turn off mysh post prompting.'
42
+ COMMAND_LINE.add_action(NoPostpromptOption.new('--no-post-prompt', desc))
43
+ COMMAND_LINE.add_action(NoPostpromptOption.new('-npp', desc))
44
+ end
@@ -0,0 +1,44 @@
1
+ # coding: utf-8
2
+
3
+ #* mysh/internal/actions/command_line/prompt.rb -- The mysh pre_prompt command.
4
+ module Mysh
5
+
6
+ #* mysh/internal/actions/command_line/pre_prompt.rb -- The mysh pre_prompt command.
7
+ class PrepromptOption < CommandOption
8
+
9
+ #Skip over the argument for pre_boot.
10
+ def pre_boot(read_point)
11
+ get_arg(read_point)
12
+ end
13
+
14
+ #Execute the prompt command line option.
15
+ #<br>Endemic Code Smells
16
+ #* :reek:UtilityFunction
17
+ def post_boot(read_point)
18
+ MNV[:pre_prompt] = get_arg(read_point)
19
+ end
20
+
21
+ end
22
+
23
+ #Add the pre prompt command line option to the library.
24
+ desc = 'Set the mysh pre prompt to "str".'
25
+ COMMAND_LINE.add_action(PrepromptOption.new('--pre-prompt "str"', desc))
26
+ COMMAND_LINE.add_action(PrepromptOption.new('-pr "str"', desc))
27
+
28
+ #* mysh/internal/actions/command_line/pre_prompt.rb -- The mysh command no pre_prompt.
29
+ class NoPrepromptOption < CommandOption
30
+
31
+ #Execute the no pre_prompt command line option.
32
+ #<br>Endemic Code Smells
33
+ #* :reek:UtilityFunction
34
+ def post_boot(_args)
35
+ MNV[:pre_prompt] = ""
36
+ end
37
+
38
+ end
39
+
40
+ #Add the prompt command line option to the library.
41
+ desc = 'Turn off mysh pre prompting.'
42
+ COMMAND_LINE.add_action(NoPrepromptOption.new('--no-pre-prompt', desc))
43
+ COMMAND_LINE.add_action(NoPrepromptOption.new('-npr', desc))
44
+ end
@@ -0,0 +1,42 @@
1
+ # coding: utf-8
2
+
3
+ #* mysh/internal/actions/command_line/prompt.rb -- The mysh prompt command.
4
+ module Mysh
5
+
6
+ #* mysh/internal/actions/command_line/prompt.rb -- The mysh prompt command.
7
+ class PromptOption < CommandOption
8
+
9
+ #Skip over the argument for pre_boot.
10
+ def pre_boot(read_point)
11
+ get_arg(read_point)
12
+ end
13
+
14
+ #Execute the prompt command line option.
15
+ def post_boot(read_point)
16
+ MNV[:prompt] = get_arg(read_point)
17
+ end
18
+
19
+ end
20
+
21
+ #Add the prompt command line option to the library.
22
+ desc = 'Set the mysh prompt to "str".'
23
+ COMMAND_LINE.add_action(PromptOption.new('--prompt "str"', desc))
24
+ COMMAND_LINE.add_action(PromptOption.new('-p "str"', desc))
25
+
26
+ #* mysh/internal/actions/command_line/prompt.rb -- The mysh prompt command no prompt.
27
+ class NoPromptOption < CommandOption
28
+
29
+ #Execute the no prompt command line option.
30
+ #<br>Endemic Code Smells
31
+ #* :reek:UtilityFunction
32
+ def post_boot(_args)
33
+ MNV[:prompt] = ""
34
+ end
35
+
36
+ end
37
+
38
+ #Add the prompt command line option to the library.
39
+ desc = 'Turn off mysh prompting.'
40
+ COMMAND_LINE.add_action(NoPromptOption.new('--no-prompt', desc))
41
+ COMMAND_LINE.add_action(NoPromptOption.new('-np', desc))
42
+ end
@@ -0,0 +1,19 @@
1
+ # coding: utf-8
2
+
3
+ #* mysh/internal/actions/command_line/quit.rb -- The mysh quit command.
4
+ module Mysh
5
+
6
+ #* mysh/internal/actions/command_line/quit.rb -- The mysh quit command.
7
+ class QuitOption < CommandOption
8
+
9
+ #Execute the quit command line option.
10
+ def post_boot(_args)
11
+ exit
12
+ end
13
+
14
+ end
15
+
16
+ #Add the quit command line option to the library.
17
+ desc = 'Quit out of the mysh program.'
18
+ COMMAND_LINE.add_action(QuitOption.new('--quit', desc))
19
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+
3
+ #* mysh/internal/actions/command_line/usage.rb -- The mysh help command usage.
4
+ module Mysh
5
+
6
+ #* mysh/internal/actions/command_line/usage.rb -- The mysh help command usage.
7
+ class UsageOption < CommandOption
8
+
9
+ #Execute the help command line option. (Punt to error handler with no msg)
10
+ def pre_boot(_args)
11
+ raise ""
12
+ end
13
+
14
+ end
15
+
16
+ #Add the usage command line option to the library.
17
+ desc = 'Display mysh usage info and exit.'
18
+
19
+ USAGE = UsageOption.new('--help', desc)
20
+ COMMAND_LINE.add_action(USAGE)
21
+ COMMAND_LINE.add_action(UsageOption.new('-h', desc))
22
+ COMMAND_LINE.add_action(UsageOption.new('-?', desc))
23
+ end
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+
3
+ #* mysh/internal/actions/comment.rb -- A mysh internal comment.
4
+ module Mysh
5
+
6
+ #* mysh/internal/actions/comment.rb -- A mysh internal comment.
7
+ class MyshComment < Action
8
+
9
+ #Ignore a comment.
10
+ def call(_args)
11
+ :internal
12
+ end
13
+
14
+ end
15
+
16
+ #Add comments to the library.
17
+ desc = 'A mysh comment. No action taken'
18
+ MYSH_COMMENT = MyshComment.new('#<stuff>', desc)
19
+ COMMANDS.add_action(MYSH_COMMENT)
20
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+
3
+ #* mysh/internal/actions/elapsed.rb -- The mysh internal elapsed command.
4
+ module Mysh
5
+
6
+ #* mysh/internal/actions/elapsed.rb -- The mysh internal elapsed command.
7
+ class TimedCommand < Action
8
+
9
+ #Execute the elapsed command.
10
+ #<br>Endemic Code Smells
11
+ #* :reek:DuplicateMethodCall -- needed due to time side effects.
12
+ def call(str)
13
+ start_time = Time.now
14
+ Mysh.try_execute_command(str[1..-1])
15
+ end_time = Time.now
16
+
17
+ puts "Elapsed execution time = #{"%.3f" % (end_time - start_time)} seconds."
18
+ :internal
19
+ end
20
+
21
+ end
22
+
23
+ #Add the elapsed commands to the library.
24
+ desc = 'Execute a command and then display the elapsed time.'
25
+ TIMED_COMMAND = TimedCommand.new('%<command>', desc)
26
+ COMMANDS.add_action(TIMED_COMMAND)
27
+ end