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

Importer DSL Idea #502

Open
ashmaroli opened this issue Dec 1, 2022 · 2 comments
Open

Importer DSL Idea #502

ashmaroli opened this issue Dec 1, 2022 · 2 comments
Labels

Comments

@ashmaroli
Copy link
Member

Jotting this down for feedback and for future reference.

# frozen_string_literal: true

module JekyllImport
  class Foobar < Importers::Importer
    title "Foo Bar"
    needs "sequel", "sqlite3", "safe_yaml"

    set_option "dbname",   "--dbname DB",   "Database name."
    set_option "user",     "--user NAME",   "Database user name."
    set_option "password", "--password PW", "Database user's password. (default: '')"
    set_option "host",     "--host HOST",   "Database host name. (default: 'localhost')"
    set_option "port",     "--port NUM",    "Database port. (default: '8080')"

    class << self
      # Entry point.
      # Mandatory public method. Will raise exception if not defined.
      def process(options)
        # ...
      end

      # Optional public method
      def validate(options)
        # ...
      end

      private

      # ...
    end
  end
end
@parkr
Copy link
Member

parkr commented Mar 5, 2023

I like this! It'd be really cool if we could mark the options as required inline instead of needing to do that in the validate func.

@ashmaroli
Copy link
Member Author

ashmaroli commented Mar 6, 2023

It'd be really cool if we could mark the options as required inline instead of needing to do that in the validate func.

OptionParser itself doesn't have a builtin mechanism to enforce required options.
However, if given a Class for Type Coercion, it can throw error when passed an empty string. For example,

cmd.option "--export-file PATH", "Path to export file."

will not fail when --export-file is omitted or passed an empty string. But,

cmd.option "--export-file PATH", String, "Path to export file."

will fail when --export-file is passed an empty string. Omission is legal. (as of bundled version in Ruby 2.7.x).

Update: Just found out that we can define custom type coercion via OptionParser#accept 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants