simple_cli 0.0.2 → 0.0.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5768544b4fccdc2dbb36b729e523b1aa81b74766
4
- data.tar.gz: 940525cc250b4e256fa69c7e4233f34a01afb8c9
3
+ metadata.gz: 6d5397a030dc3cbe0065b382f3c82396e11dd3bb
4
+ data.tar.gz: b9abf7884e67d5714cde8a845613868bae36023a
5
5
  SHA512:
6
- metadata.gz: 96ac5469e10c38bf40de54005938e28048ae4030d32ca9ae5f7246abf1180e799598b748bc6395e68b9f3d0ea9143fef678a0aabeecc9b507bb0eb586595d02a
7
- data.tar.gz: cb8b4fb40c64b396cf6a307b5fe64de3ce9849e8a209b267f152a4d5aba1d4a442167746f4a349a06f6d192a9cde81224042dec0e14c439eb28be92298a9f49d
6
+ metadata.gz: e9dd2c16e2d19fbdfa952889c36515fe73269d54d5212e4c9dc4848da972ee32c81c75578207a4a5c7ebac973c0ea8df99145fbb0dc9d5747db36fe1eae4840a
7
+ data.tar.gz: 3d1a6b579b22c0fb80af1a5c73db31f945712ada8c15f116a8cbbc54f58c0762f9addf514fd76174b367ae1fb8c84428051e2b69bb8186647c5f68940b438df9
data/README.md CHANGED
@@ -1,2 +1,61 @@
1
1
  # simple_cli
2
- A very opinionated Ruby library for building CLI tools. Extracted from my simple_configure project.
2
+ A very opinionated Ruby library for parsing CLI input. Extracted from my simple_configure project.
3
+
4
+ ## What exactly is simple_cli?
5
+ simple_cli at its core is a command line parsing library combined with a little bit of magic. It takes a very declarative approach to building CLI tools. You describe your CLI tool in a json file. simple_cli takes that json file and Ruby's `ARGV` as parameters and builds a scaffold for your CLI tool. It outputs a Hash of options parsed from the CLI input if the CLI input is valid or else it outputs a help message. It allows you to focus only on the core of your CLI tool instead of focusing on parsing CLI options.
6
+
7
+ ## Installation
8
+ You can install simple_cli by running
9
+ ```
10
+ gem install simple_cli
11
+ ```
12
+
13
+ ## Documentation
14
+ To use simple_cli in a project, require the `simple_cli` module
15
+ ```ruby
16
+ require 'simple_cli`
17
+ ```
18
+
19
+ This should provide you access to the `SimpleCli` module. `SimpleCli` module has a `SimpleCliBuilder` class that can be used to develop a CLI parser. `SimpleCliBuilder` takes in two arguments. First one is a json file and the second one is the `ARGV` parameter from Ruby. Here is a simple CLI tool using this method
20
+
21
+ ```ruby
22
+ #!/usr/bin/env ruby
23
+
24
+ # This code was taken from https://github.com/lsegal/yard/blob/master/bin/yard. The code is licensed under the MIT License.
25
+
26
+ # We do all this work just to find the proper load path
27
+ path = __FILE__
28
+ while File.symlink?(path)
29
+ path = File.expand_path(File.readlink(path), File.dirname(path))
30
+ end
31
+ $:.unshift(File.join(File.dirname(File.expand_path(path)), '..', 'lib'))
32
+
33
+ require 'testcli'
34
+ require 'simple_cli'
35
+
36
+ parsed_cli_args = SimpleCliBuilder.new("testcli.json",ARGV)
37
+ TestCli.run(parsed_cli_args)
38
+ ```
39
+
40
+ ### JSON File Format
41
+
42
+ This file is at the heart of simple_cli. You declare all the parts of your CLI tool in this JSON file. This file helps the `SimpleCliBuilder` to extract information from the CLI input and present it to the tool in a meaningful way. The following are the available options that can be included inside the JSON file.
43
+
44
+ 1. **name** - required - Name of the command line tool. This will be displayed in the automatically generated help message.
45
+ 2. **description** - required - Description of the command line tool. This will be displayed in the automatically generated help message.
46
+ 3. **commands** - required - This parameter should be a JSON object with all the commands in the command line tool.
47
+
48
+ ## Errors
49
+ `simple_cli` is built from the ground up to make your command line tool error free. It will do a lot of error checking while your tool is in development. So you might run into the following errors when you use `simple_cli` with your CLI tool. The error messages are in the process of being rewritten to make it explicit that these are errors caused by developers not the users.
50
+
51
+ ### JSONFileNonExistantError
52
+ This error indicates that the JSON file provided doesn't exist. With out the JSON file, `simple_cli` cannot proceed any further.
53
+
54
+ ### UnParsableJSONError
55
+ This error indicates that the JSON file provided is not a valid JSON. Please use a tool like http://jsonlint.com/ to fix the problems with your JSON.
56
+
57
+ ### EmptyJSONError
58
+ This error indicates that the JSON file provided is empty.There is nothing in a empty JSON file to configure `simple_cli`. So please add some configurations into that JSON file.
59
+
60
+ ### NoNameError
61
+ This error indicates that the `name` parameter is missing from the JSON file. The `name` parameter is very important because it will be displayed in the automatically generated help message.
@@ -1,18 +1,100 @@
1
1
  require 'json'
2
+ require_relative 'error'
3
+
2
4
  module SimpleCli
3
- class SimpleCliBuilder
4
- def initialize(config_file)
5
- @allowable_arguments = config_file
6
- end
5
+ class SimpleCliBuilder
6
+ def initialize(config_file, argv = [])
7
+ @config, @available_commands = parse_allowable_arguments(config_file)
8
+ @raw_argv = argv
9
+ @parsed_argv = parse_argv
10
+ end
11
+
12
+ private
13
+
14
+ def error_message(command)
15
+ return "Error: #{command} is not a valid command/option/argument!"
16
+ end
17
+
18
+ def build_help_message(config)
19
+ message = "Usage: #{config["name"]}\n"
20
+ message += "\n"
21
+ message += "#{config["description"]}\n"
22
+ message += "\n"
23
+ return message
24
+ end
25
+
26
+ def version_message(config)
27
+ message = "#{config["name"]} version #{config["version"]}\n\n"
28
+ return message
29
+ end
30
+
31
+ def parse_allowable_arguments(config_file)
32
+ user_note = "User Note: The developer made a mistake. So this tool is not currently functional!"
33
+ if File.exist?(config_file)
34
+ file = File.read(config_file)
35
+ else
36
+ raise JSONFileNonExistantError, "Developer Note: The JSON file you provided doesn't exist. Please provide a valid JSON file!\n" + user_note
37
+ end
38
+
39
+ begin
40
+ json_data = JSON.parse(file)
41
+ rescue
42
+ raise UnParsableJSONError, "Developer Note: The JSON file you provided is not a valid JSON file. Please fix this issue!\n" + user_note
43
+ end
44
+ return verify_config_file(json_data)
45
+ end
46
+
47
+ def verify_config_file(config)
48
+ user_note = "User Note: The developer made a mistake. So this tool is not currently functional!"
49
+
50
+ if config == {}
51
+ raise EmptyJSONError, "Developer Note: The JSON file you provided is empty. Please fix this issue!\n" + user_note
52
+ end
53
+
54
+ if config["name"].nil?
55
+ raise NoNameError, "Developer Note: A CLI app requires a name. But no name has been provided. Please fix this issue!\n" + user_note
56
+ elsif config["description"].nil?
57
+ raise NoDescriptionError, "Developer Note: A CLI app requires a description. But no description has been provided. Please fix this issue!\n" + user_note
58
+ elsif config["commands"].nil?
59
+ raise NoCommandsError, "Developer Note: A CLI app requires a list of commands. But no commands have been provided. Please fix this issue! If your CLI tool doesn't take any commands, please use the no_command option inside the commands object to indicate that behavior\n" + user_note
60
+ elsif config["commands"] == {}
61
+ raise EmptyCommandsError, "Developer Note: A CLI app requires a list of commands. You have provided a commands object. But it is empty. Please fix this issue!\n" + user_note
62
+ end
63
+
64
+ available_commands = {}
7
65
 
8
- private
66
+ config["commands"].keys.each do |key|
67
+ if key != "no_command"
68
+ available_commands[key] = key
69
+ command = config["commands"][key]
70
+ unless command["alias"].nil?
71
+ available_commands[command["alias"]] = key
72
+ end
73
+ end
74
+ end
9
75
 
10
- def parse_allowable_arguments(config_file)
11
- return JSON.parse(config_file)
12
- end
76
+ return config, available_commands
13
77
 
14
- def verify_config_file
78
+ end
15
79
 
16
- end
80
+ def parse_argv
81
+ parsed_argv = {}
82
+ @raw_argv.each do |arg|
83
+ case arg
84
+ when "-h", "--help", "help"
85
+ puts build_help_message(@config)
86
+ when "-v", "--version", "version"
87
+ puts version_message(@config)
88
+ when !@available_commands[arg].nil?
89
+ parsed_argv[available_commands[arg]] = true
90
+ else
91
+ puts "\n"
92
+ puts error_message(arg)
93
+ puts "\n"
94
+ abort
95
+ end
96
+ end
97
+ return parsed_argv
98
+ end
17
99
  end
18
100
  end
@@ -0,0 +1,20 @@
1
+ class JSONFileNonExistantError < StandardError
2
+ end
3
+
4
+ class NoNameError < StandardError
5
+ end
6
+
7
+ class UnParsableJSONError < StandardError
8
+ end
9
+
10
+ class EmptyJSONError < StandardError
11
+ end
12
+
13
+ class NoDescriptionError < StandardError
14
+ end
15
+
16
+ class NoCommandsError < StandardError
17
+ end
18
+
19
+ class EmptyCommandsError < StandardError
20
+ end
@@ -1,3 +1,3 @@
1
1
  module SimpleCli
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adhithya Rajasekaran
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-14 00:00:00.000000000 Z
11
+ date: 2015-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -69,6 +69,7 @@ files:
69
69
  - Rakefile
70
70
  - lib/simple_cli.rb
71
71
  - lib/simple_cli/core.rb
72
+ - lib/simple_cli/error.rb
72
73
  - lib/simple_cli/version.rb
73
74
  - simple_cli.gemspec
74
75
  homepage: ''