Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Reorganize option parsing for Crystal 0.23 compatibility #24

Merged
merged 2 commits into from
Aug 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Fixed
- Bug on colorizing output ([#22](https://github.com/porras/crul/pull/22), thanks [straight-shoota](https://github.com/straight-shoota))

### Other
- Crystal 0.23 compatibility ([#24](https://github.com/porras/crul/pull/24)

## [0.4.1](https://github.com/porras/crul/compare/0.4.0...0.4.1)
### Fixed
- Bug on formatting floats ([#18](https://github.com/porras/crul/issues/18))
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ You don't need this if you installed via Homebrew (it's automatic).

## Development

You'll need [Crystal 0.20](http://crystal-lang.org/docs/installation/index.html) installed (it might work with older
You'll need [Crystal 0.23](http://crystal-lang.org/docs/installation/index.html) installed (it might work with older
or newer versions, but that's the one that's tested).

After checking out the repo (or decompressing the tarball with the source code), run `shards` to get
Expand Down
4 changes: 2 additions & 2 deletions src/crul.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Crul
options = Options.parse(argv)

if options.help?
output.puts options.parser
output.puts Options::USAGE
return true
end

Expand All @@ -14,7 +14,7 @@ module Crul
end

if options.errors.any?
output.puts options.parser
output.puts Options::USAGE

output.puts "Errors:"
options.errors.each do |error|
Expand Down
53 changes: 43 additions & 10 deletions src/options.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,52 @@ module Crul
@errors = [] of Exception
end

USAGE = <<-USAGE
Usage: crul [method] URL [options]

HTTP methods (default: GET):
get, GET Use GET
post, POST Use POST
put, PUT Use PUT
delete, DELETE Use DELETE

HTTP options:
-d DATA, --data DATA Request body
-d @file, --data @file Request body (read from file)
-H HEADER, --header HEADER Set header
-a USER:PASS, --auth USER:PASS Basic auth
-c FILE, --cookies FILE Use FILE as cookie store (reads and writes)

Response formats (default: autodetect):
-j, --json Format response as JSON
-x, --xml Format response as XML
-p, --plain Format response as plain text

Other options:
-h, --help Show this help
-V, --version Display version
USAGE

def self.parse(args)
new.tap do |options|
options.parser = OptionParser.parse(args) do |parser|
parser.banner = "Usage: crul [method] URL [options]"

parser.separator
parser.separator "HTTP methods (default: GET):"
parser.on("get", "GET", "Use GET") { options.method = Methods::GET }
parser.on("post", "POST", "Use POST") { options.method = Methods::POST }
parser.on("put", "PUT", "Use PUT") { options.method = Methods::PUT }
parser.on("delete", "DELETE", "Use DELETE") { options.method = Methods::DELETE }
case args.first?
when "get", "GET"
args.shift
options.method = Methods::GET
when "post", "POST"
args.shift
options.method = Methods::POST
when "put", "PUT"
args.shift
options.method = Methods::PUT
when "delete", "DELETE"
args.shift
options.method = Methods::DELETE
else # it's the default
options.method = Methods::GET
end

parser.separator
options.parser = OptionParser.parse(args) do |parser|
parser.separator "HTTP options:"
parser.on("-d DATA", "--data DATA", "Request body") do |body|
options.body = if body.starts_with?('@')
Expand Down