Add Pull.cli_options

I came across the pattern in this diff while browsing the
[rake](https://github.com/ruby/rake/)
source code, and I thought it was worth repeating in quran-json.
This commit is contained in:
0x1eef 2023-03-14 19:13:49 -03:00 committed by Robert
parent 4ba407021e
commit 3f1ed58432
2 changed files with 30 additions and 14 deletions

View file

@ -43,9 +43,10 @@ the content of The Quran in multiple languages.
*Usage* *Usage*
Usage: quran-json pull [OPTIONS] Usage: quran-json pull [OPTIONS]
-l, --locale LOCALE ar, en, pt, fa, nl, fr, or it (default: en) -l, --locale LOCALE ar, en, pt, fa, nl, fr, or it (default: en)
-r, --replace Replace existing JSON files (default: no) -r, --replace Replace existing JSON files (default: no)
-u, --update Only update the surah metadata (implies -r, default: no) -u, --update Replace surah metadata with an updated copy (implies -r, default: no)
<p align="left"> <p align="left">
<strong>Thanks</strong> <strong>Thanks</strong>
@ -53,12 +54,12 @@ the content of The Quran in multiple languages.
First and foremost, Alhamdulillah. First and foremost, Alhamdulillah.
Thanks to the following websites for making the content available to download: Thanks to the following websites for providing the downloadable content:
* https://searchtruth.com for the original Arabic, and the Italian translation. * https://searchtruth.com for the original Arabic, and the Italian translation.
* https://quran.com for the English, Farsi, Portuguese, Dutch, and French translations. * https://quran.com for the English, Farsi, Portuguese, Dutch, and French translations.
And thanks to the translators of the content: And sincere thanks to the translators of the content:
* _Dr. Mustafa Khattab_ for the English translation. * _Dr. Mustafa Khattab_ for the English translation.
* _Hussein Taji Kal Dari_ for the Farsi translation. * _Hussein Taji Kal Dari_ for the Farsi translation.
@ -72,4 +73,4 @@ And thanks to the translators of the content:
The "source code" is released under the [GPL](./LICENSE) license. The "source code" is released under the [GPL](./LICENSE) license.
<br> <br>
The content is copyrighted to the translators (named above). The content is copyrighted to the translators (listed above).

View file

@ -15,20 +15,35 @@ class Quran::JSON::Pull
def self.cli(argv) def self.cli(argv)
op = nil op = nil
options = Ryo({locale: "en", replace: false, update: false}) result = Ryo({locale: "en", replace: false, update: false})
OptionParser.new(nil, 26, " " * 2) do |o| OptionParser.new(nil, 22, " " * 2) do |o|
o.banner = "Usage: quran-json pull [OPTIONS]"
op = o op = o
o.on("-l", "--locale LOCALE", "ar, en, pt, fa, nl, fr, or it (default: en)") op.banner = "Usage: quran-json pull [OPTIONS]"
o.on("-r", "--replace", "Replace existing JSON files (default: no)") cli_options.each { op.on(*_1) }
o.on("-u", "--update", "Only update the surah metadata (implies -r, default: no)") end.parse(argv, into: result)
end.parse(argv, into: options) result
options
rescue rescue
puts op.help puts op.help
exit exit
end end
def self.cli_options
[
[
"-l", "--locale LOCALE",
"ar, en, pt, fa, nl, fr, or it (default: en)"
],
[
"-r", "--replace",
"Replace existing JSON files (default: no)"
],
[
"-u", "--update",
"Replace surah metadata with an updated copy (implies -r, default: no)"
]
]
end
def initialize(options) def initialize(options)
@options = options @options = options
@source = sources[options.locale] @source = sources[options.locale]