Pass commandline arguments to application
To pass command-line arguments to an application you're running, execute dub run <dub arguments> -- <application arguments>
.
Single-file packages
For small or script-like applications, DUB supports a special mode where the whole package is contained in a single .d file. The package recipe description can be embedded into code comments within the file:
#!/usr/bin/env dub /+ dub.sdl: name "hello" +/ void main() { import std.stdio : writeln; writeln("Hello, World!"); }
This application can be executed by running dub run --single hello.d
, or just compiled with dub build --single hello.d
.
In addition to the normal method of passing commandline arguments, you can use the shorthand dub hello.d <arguments to hello>
. This shorthand and the optional shebang allow you to run applications via ./hello <arguments>
from your shell if you set the executable bit of the file.
Single-file packages cannot be used to create library packages.
Using dependencies not published on the registry
There are various options to use packages not published on the registry:
dub add-path
- Adds a local search directory. All direct subdirectories will be searched for packages. Local search directories are always searched before looking up packages from the registry.
dub add-local
- Similar to the above, but only adds a single package directory.
dub add-override
- Overrides a certain version or version range of a package with another version, branch, or path. This can be used to change dependencies system-wide without modifying a package's description or selection files.
- Path-based dependencies
- Package descriptions in the
dub.json/dub.sdl
can specify a path instead of a version; this can be used with Git submodules or subtrees, or with a known directory layout, to use arbitrarily defined versions of a dependency. Note that this should only be used for non-public packages. - Path-based selections
- You can specify arbitrary versions, branches, and paths in the
dub.selections.json
file, even if they contradict the dependency specification of the packages involved (note that DUB will output a warning in that case).
Execute dub <command> -h
for more information on these commands.
Managing subpackages
For adding subpackages from the registry, execute dub add <packagename>:<subpackage>
If you need to compile a subpackage only, use dub build :<subpackage>
Conditional compilation according to dependency presence
Projects can have additional special dependencies for specific configurations.
"configurations": [ { "name": "myconfig", "dependencies": { "mydep": { "version": "*" } } } ]
DUB provides version identifier of dependencies for conditional compilation with version conditions. Have_<dependency>
version identifier can be used for conditional compilation.
NOTE: Special characters other than identifier characters ([a-z, A-Z, 0-9, _]
) in dependency name are replaced with _
character in version identifier.
version (Have_mydep) { import mydep; }