This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 201
Form Input
Kevin O'Sullivan edited this page Jul 8, 2021
·
6 revisions
Forms are a way of helping format and validate input into a command for situations where there is more than a couple of inputs into a command.
Forms will allow you to declare positional inputs as well as flag inputs. The following code snippet will add a project_name
positional argument, a host
, and a disablewebpack
flag arguments.
This means it can be used as shopify foo create project_name --host=http://google.com --disablewebpack
# lib/project_types/foo/forms/create.rb
module Foo
module Forms
class Create < ShopifyCli::Form
positional_arguments :project_name
flag_arguments :host, :disablewebpack
def ask
# Add defaults if nil, validate existing values
end
end
end
end
Setup your project to load it
# frozen_string_literal: true
module Rails
class Project < ShopifyCli::ProjectType
end
module Forms
# autoload the class so ruby knows where to load it from
autoload :Create, Project.project_filepath('forms/create')
end
end
And in the command
# lib/project_types/foo/commands/create.rb
module Foo
class Command
class Create < ShopifyCli::SubCommand
options do |parser, flags|
parser.on('--host=HOST') { |t| flags[:host] = t }
parser.on('--disablewebpack') { |url| flags[:disablewebpack] = true }
end
def call(args, _name)
form = Forms::Create.ask(@ctx, args, options.flags)
# if the form returns nil that means there was a validation error
# so you should abort the command
return @ctx.puts(self.class.help) if form.nil?
@ctx.puts("received: #{form.project_name} #{form.host} #{form.disablewebpack}")
end
end
end
end