{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","results":{"codes":[]},"settings":"","params":[]},"next":{"description":"","pages":[]},"title":"Command","type":"basic","slug":"command","excerpt":"","body":"Commands are triggered from the command line and are a good place to put your apps maintenance utilities. \n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Creating a Command\"\n}\n[/block]\nThe following command creates test users.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"struct CreateTestUser: Command {\\n    static let id: String = \\\"create-test-user\\\"\\n\\n    static let signature: [Signature] = [\\n        Argument(\\\"name\\\"),\\n        Option(\\\"repeat\\\")\\n    ]\\n\\n    static let help: [String] = [\\n        \\\"creates test users\\\"\\n    ]\\n\\n    let app: Application\\n    init(app: Application) {\\n        self.app = app\\n    }\\n\\n    func run() throws {\\n        guard let name = try argument(\\\"name\\\").string else {\\n            throw CommandError.custom(\\\"Name wasn't string\\\")\\n        }\\n\\n        let amount = option(\\\"repeat\\\").int ?? 1\\n\\n        info(\\\"Creating \\\\(amount) user(s)\\\")\\n\\n        for i in 0 ..< amount {\\n            let user = User(name: name)\\n            try user.save()\\n        }\\n    }\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nThis command can be run on the command line using `vapor run create-test-user John --repeat=5`\n\nYou can also run it directly from the executable like `.build/debug/App create-test-user John`\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"app.commands.append(CreateTestUser)\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nYou can then add your command to the application.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Options and Arguments\"\n}\n[/block]\nUse the static `signature` property to outline what arguments and options your command takes. This will be used to generate the signature in the help command. It will also be used to ensure your command receives the correct number of arguments.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"let option = option(\\\"option-name\\\") // Polymorphic?\\nlet argument = try argument(\\\"argument-name\\\") // Polymorphic\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nAlthough you can be sure arguments will be provided to your command, you might need to cast them to different types. Because of this, you should still provide a `guard` statement to ensure the correct type.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Printing\"\n}\n[/block]\nThe following methods are available for printing.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"print(\\\"Plain message\\\")\\ninfo(\\\"Informational message\\\")\\nwarning(\\\"Warning message\\\")\\nerror(\\\"Error message\\\")\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Interaction\"\n}\n[/block]\nYou can interact with the user and receive input using the `ask()` and `confirm()` commands.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"guard let age = ask(\\\"What is your age?\\\").int else {\\n    throw CommandError.custom(\\\"Age was not an integer\\\")\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"guard confirm(\\\"Are you sure?\\\") else {\\n    throw CommandError.custom(\\\"You were not sure!\\\")\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nThe confirm command will accept `yes` or `no` as well as `y` or `n` and is case-insensitive.","updates":[],"order":999,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"57687f73ac31780e000cc619","user":"57086f3d447c410e00221bd1","version":{"version":"0.12","version_clean":"0.12.0","codename":"Client Clyde","is_stable":false,"is_beta":true,"is_hidden":false,"is_deprecated":true,"categories":["5768701b72cd4c0e000fad19","5768701b72cd4c0e000fad1a","5768701b72cd4c0e000fad1b","5768701b72cd4c0e000fad1c"],"_id":"5768701b72cd4c0e000fad18","releaseDate":"2016-06-20T22:37:15.641Z","__v":1,"createdAt":"2016-06-20T22:37:15.641Z","project":"57086fe855a4690e005de407"},"category":{"sync":{"isSync":false,"url":""},"pages":[],"title":"Guide","slug":"getting-started","order":1,"from_sync":false,"reference":false,"_id":"5768701b72cd4c0e000fad1a","createdAt":"2016-04-09T04:31:52.489Z","__v":0,"version":"5768701b72cd4c0e000fad18","project":"57086fe855a4690e005de407"},"createdAt":"2016-06-20T23:42:43.577Z","project":"57086fe855a4690e005de407","__v":22,"githubsync":""}
Commands are triggered from the command line and are a good place to put your apps maintenance utilities. [block:api-header] { "type": "basic", "title": "Creating a Command" } [/block] The following command creates test users. [block:code] { "codes": [ { "code": "struct CreateTestUser: Command {\n static let id: String = \"create-test-user\"\n\n static let signature: [Signature] = [\n Argument(\"name\"),\n Option(\"repeat\")\n ]\n\n static let help: [String] = [\n \"creates test users\"\n ]\n\n let app: Application\n init(app: Application) {\n self.app = app\n }\n\n func run() throws {\n guard let name = try argument(\"name\").string else {\n throw CommandError.custom(\"Name wasn't string\")\n }\n\n let amount = option(\"repeat\").int ?? 1\n\n info(\"Creating \\(amount) user(s)\")\n\n for i in 0 ..< amount {\n let user = User(name: name)\n try user.save()\n }\n }\n}", "language": "swift" } ] } [/block] This command can be run on the command line using `vapor run create-test-user John --repeat=5` You can also run it directly from the executable like `.build/debug/App create-test-user John` [block:code] { "codes": [ { "code": "app.commands.append(CreateTestUser)", "language": "swift" } ] } [/block] You can then add your command to the application. [block:api-header] { "type": "basic", "title": "Options and Arguments" } [/block] Use the static `signature` property to outline what arguments and options your command takes. This will be used to generate the signature in the help command. It will also be used to ensure your command receives the correct number of arguments. [block:code] { "codes": [ { "code": "let option = option(\"option-name\") // Polymorphic?\nlet argument = try argument(\"argument-name\") // Polymorphic", "language": "swift" } ] } [/block] Although you can be sure arguments will be provided to your command, you might need to cast them to different types. Because of this, you should still provide a `guard` statement to ensure the correct type. [block:api-header] { "type": "basic", "title": "Printing" } [/block] The following methods are available for printing. [block:code] { "codes": [ { "code": "print(\"Plain message\")\ninfo(\"Informational message\")\nwarning(\"Warning message\")\nerror(\"Error message\")", "language": "swift" } ] } [/block] [block:api-header] { "type": "basic", "title": "Interaction" } [/block] You can interact with the user and receive input using the `ask()` and `confirm()` commands. [block:code] { "codes": [ { "code": "guard let age = ask(\"What is your age?\").int else {\n throw CommandError.custom(\"Age was not an integer\")\n}", "language": "swift" } ] } [/block] [block:code] { "codes": [ { "code": "guard confirm(\"Are you sure?\") else {\n throw CommandError.custom(\"You were not sure!\")\n}", "language": "swift" } ] } [/block] The confirm command will accept `yes` or `no` as well as `y` or `n` and is case-insensitive.