Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a (slightly) better CLI #10

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions Makefile
@@ -1,5 +1,6 @@
crystal_lib: $(shell find . -name "*.cr")
crystal build src/crystal_lib.cr
all: cli
cli: $(shell find . -name "*.cr")
crystal build src/cli.cr -o crystal_lib

clean:
rm -rf .crystal crystal_lib
17 changes: 17 additions & 0 deletions src/cli.cr
@@ -0,0 +1,17 @@
require "./clang"
require "./crystal_lib"
require "option_parser"
output_file = ""
OptionParser.parse! do |parser|
parser.banner = "Usage: crystal_lib [arguments]"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some sort of clarification that the last argument should be the input file is missing here.

parser.on("-o FILE", "--output=FILE", "The file to output the generated code to.") { |filename| output_file = filename }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarify the default if it's not specified (for example: "defaults to output.cr"). Actually, it'd be nice if the default took the same name as the input file, except with the cr extension.

parser.on("-h", "--help", "Show this help") { puts parser; Process.exit(0) }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should provide an option to print to the console, as it works currently.

end
if output_file == ""
output_file = "output.cr"
end

node = Crystal::Parser.parse(ARGF.gets_to_end)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks when the user doesn't provide an input file. It should show usage instead.

visitor = CrystalLib::LibTransformer.new
transformed = node.transform visitor
File.write(output_file, transformed)