How to add custom arguments to Vapor's serve command

The default Vapor serve command accept only hostname, port and bind arguments. I want to extend it by adding some custom arguments. I have tried to use a AsyncCommand to define a custom command, but I have to manually start the server in this case. In fact, the default ServeCommand does a lot of work to start the server, and it even defines functions for stopping the server. So if we want to start the server using custom AsyncCommand, we basically have to re-implemented all those stuffs (I can't figure out where the asyncShutdown method of ServeCommand is called). I have also tried to combine Vapor with swift-argument-parser, but when the Vapor is doing its own argument parsing job, it throws exceptions when it sees those new arguments. So what's the best way to handle such requirement?

As a workaround, I often use .env file in the project root folder and access its values in the configure.swift like this:

let sqlitePath = Environment.get("DATABASE_PATH") ?? "db.sqlite"

Vapor 4 was built in a time when argument parser and service lifecycle didn't exist so we handled (and need to continue to do so to avoid breaking changes) all of that. If you need to implement custom arguments then you'll need to copy and paste the ServerCommand body (IIRC asyncShutdown is called in the lifecycle handler).

But what do you want to pass in that you can't with environment variables?

1 Like