Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

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 valuesendendendend

Setup 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')endend

And 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

Clone this wiki locally