Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Jun 1, 2023. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 214
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.rbmoduleFoomoduleFormsclassCreate < ShopifyCli::Formpositional_arguments:project_nameflag_arguments:host,:disablewebpackdefask# Add defaults if nil, validate existing valuesendendendendSetup your project to load it
# frozen_string_literal: truemoduleRailsclassProject < ShopifyCli::ProjectTypeendmoduleForms# autoload the class so ruby knows where to load it fromautoload:Create,Project.project_filepath('forms/create')endendAnd in the command
# lib/project_types/foo/commands/create.rbmoduleFooclassCommandclassCreate < ShopifyCli::SubCommandoptionsdo |parser,flags|
parser.on('--host=HOST'){ |t| flags[:host]=t}parser.on('--disablewebpack'){ |url| flags[:disablewebpack]=true}enddefcall(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 commandreturn@ctx.puts(self.class.help)ifform.nil?@ctx.puts("received: #{form.project_name}#{form.host}#{form.disablewebpack}")endendendend