-
Notifications
You must be signed in to change notification settings - Fork 1
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
Optparse and Console output #3
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
require 'date' | ||
require 'delegate' | ||
require 'tilt' | ||
require 'optparse' | ||
|
||
require 'cells' | ||
require 'cells-erb' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,56 @@ | ||
module Ordodo | ||
class CLI | ||
def self.run!(argv) | ||
|
||
options = {} | ||
optparse = OptionParser.new do|opts| | ||
# Set a banner, displayed at the top | ||
# of the help screen. | ||
opts.banner = "Usage: ordodo [OPTIONS] myconfig.xml" | ||
|
||
# Define the options, and what they do | ||
options[:outputfile] = nil | ||
opts.on( '-o', '--output FILE', 'Write output to FILE' ) do|file| | ||
options[:outputfile] = file | ||
end | ||
|
||
options[:year] = nil | ||
opts.on( '-y', '--year YEAR', 'Specify the YEAR for which the ordo should be produced' ) do|year| | ||
options[:year] = Integer(year) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More idiomatic Ruby way of "type-casting" is by calling the subject's method: |
||
end | ||
|
||
# This displays the help screen | ||
opts.on( '-h', '--help', 'Display this screen' ) do | ||
puts opts | ||
exit | ||
end | ||
end | ||
# Parse the command-line. | ||
optparse.parse! | ||
|
||
config_path = argv[0] || die('Specify path to the configuration file.') | ||
year = argv[1]&.to_i | ||
|
||
begin | ||
|
||
if options[:outputfile] | ||
output_dir = File.dirname(options[:outputfile]) | ||
output_filename = File.basename(options[:outputfile]) | ||
else | ||
output_dir = nil | ||
output_filename = "index_" + File.basename(config_path, ".xml") + ".html" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With regard to further development it would be better to only make output directory configurable (and build the default one using the config file's name) and always force the same hardcoded file name ( |
||
end | ||
|
||
year = options[:year] | ||
|
||
begin | ||
config = | ||
File.open(config_path) do |fr| | ||
Config.from_xml(fr) do |config| | ||
config.year = year if year | ||
if output_dir | ||
config.output_dir = output_dir | ||
else | ||
config.output_dir = "ordo_#{config.year}" | ||
end | ||
config.output_filename = output_filename | ||
end | ||
end | ||
Generator.new(config).call | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,10 @@ def initialize | |
@locale = :en | ||
@temporale_options = {} | ||
@temporale_extensions = [] | ||
@calendars = nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. initialization of (In Ruby instance variables, if not explicitly assigned, have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True. |
||
@output_directory = nil | ||
@year = upcoming_year | ||
@title = 'Liturgical Calendar' | ||
@output_dir = nil | ||
@output_filename = 'ordodo_out' | ||
|
||
@loader = CalendariumRomanum::SanctoraleLoader.new | ||
|
||
|
@@ -25,7 +25,8 @@ def initialize | |
:temporale_options, | ||
:temporale_extensions, | ||
:calendars, | ||
:output_directory, | ||
:output_dir, | ||
:output_filename, | ||
:title, | ||
:year | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently the user is expected to specify year on every invocation. This change makes year optional and sensible default (upcoming year) is used if year is not specified.
I'm not completely convinced we want this.
I see an argument in favour of this change: "Any user input, for which a sensible default can be used, should be optional." And there is such a sensible default: we can expect that a typical user would use ordodo to build calendar for the upcoming year.
On the other hand, I see this particular argument as "critical". Whenever someone (in a real-life non-development scenario) uses ordodo to generate a calendar, s/he definitely wants to generate it for some particular year. Noone ever wants "some calendar, I don't really care for the year". And in such a case it makes sense to make such an argument required.
I must admit I'm undecided about this, so unless some convincing argument is added to one side or the other, I'll just merge it as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not true. The default usage is (like written in the README) without year specification. The code works exactly as before: If the year is not specified, the "upcoming year" will be used.
I think this behaviour is reasonable. In a real life situation you want to compile the calendar for the next year (in 90% of all cases).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right. I not only failed to understand my own code, but also to read my own README 😊