-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from 00JCIV00/docs
Polished Readme, Docs, & Guides
- Loading branch information
Showing
47 changed files
with
14,577 additions
and
12,406 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,48 @@ | ||
# Command | ||
A Command is a container Argument type for sub Commands, Options, and Values. It can contain any mix of those Argument types or none at all if it's to be used as a standalone Command (i.e. `covademo help`). | ||
|
||
They can be converted from and to valid Structs for easy setup and analysis. Other functions for analysis include creating a HashMap<Name, Value/Option> for Options or Values using the respective the `getOpts()` or `getVals()` methods, and using the `checkFlag()` method to simply check if an Argument type was set. Usage and Help statements for a Command can also be generated using the `usage()` and `help()` methods respectively. | ||
## Configuring a Command Type | ||
Before a Command is used within a project, a Command Type should be configured. A Command Type is used to set common-to-all properties of each Command created from it. Typically, this will cover the main Command of a project and all of its sub Commands. The easiest way to configure a Command Type is to simply use `cova.Command.Base`() which is the default Command type. To configure a custom Command type, use `cova.Command.Custom`() with a `cova.Command.Config` (`config`) which provides 7 customizations to set up the Option type, Help/Usage messages, and max sub Arguments. Once configured, the Command Type has access to all of the functions under `cova.Command.Custom` and any Command created from the Command Type similarly has access to all of the corresponding methods. | ||
|
||
Command's are meant to be set up in Comptime and used in Runtime. This means that the Command and all of its sub Argument types (Commands, Options, and Values) should be Comptime-known, allowing for proper Validation which provides direct feedback to the library user during compilation instead of preventable errors to the app user during Runtime. After they're set up and Validated, Command's are allocated to the heap for Runtime use. At this point, the data within the Command should be treated as read-only by the libary user, so the library is set up to handle initialized Commands as constants (`const`). | ||
## Setting up a Command | ||
Commands are meant to be set up in Comptime and used in Runtime. This means that the Command and all of its sub Argument types (Commands, Options, and Values) should be Comptime-known, allowing for proper Validation which provides direct feedback to the library user during compilation instead of preventable errors to the end user during Runtime. | ||
|
||
## Examples: | ||
### Direct Creation | ||
``` | ||
There are two ways to set up a Command. The first is to use Zig's standard syntax for creating a struct instance and fill in the fields of the previously configured Command Type. Alternatively, if the project has a Struct Type that can be represented as a Command, the `cova.Command.Custom.from`() function can be used to create the Command. | ||
|
||
After they're set up Commands should be Validated and Allocated to the heap for Runtime use. This is accomplished using `cova.Command.Custom.init()`. At this point, the data within the Command should be treated as read-only by the libary user, so the library is set up to handle initialized Commands as constants (`const`). | ||
|
||
## Additional Info | ||
Commands can also be converted to valid Structs for easy analysis using the `cova.Command.Custom.to`() function. Other functions for analysis include creating a String HashMap<Name, Value/Option> for Options or Values using the respective `cova.Command.Custom.getOpts`() or `cova.Command.Custom.getVals`() methods, and using the `cova.Command.Custom.checkFlag`() method to simply check if an Argument type was set. Usage and Help statements for a Command can also be generated using the `cova.Command.Custom.usage`() and `cova.Command.Custom.help`() methods respectively. | ||
|
||
## Example: | ||
```zig | ||
... | ||
pub const cova = @import("cova"); | ||
pub const CustomCommand = cova.CustomCommand(.{ global_help_prefix = "CovaDemo" }); | ||
pub const CommandT = cova.CustomCommand(.{ global_help_prefix = "CovaDemo" }); | ||
// Comptime Setup | ||
const setup_cmd: CustomCommand = .{ | ||
const setup_cmd: CommandT = .{ | ||
.name = "covademo", | ||
.description = "A demo of the Cova command line argument parser.", | ||
.sub_cmds = &.{ | ||
.{ | ||
.name = "sub_cmd", | ||
.description = "This is a Sub Command within the 'covademo' main Command.", | ||
}, | ||
command_from_elsewhere, | ||
CustomCommand.from(some_valid_struct), | ||
command_from_elsewhere, | ||
CommandT.from(SomeValidStructType), | ||
} | ||
.opts = { ... }, | ||
.vals = { ... } | ||
} | ||
pub fn main() !void { | ||
... | ||
// Runtime Use | ||
const main_cmd = try setup_cmd.init(alloc); | ||
defer main_cmd.deinit(); | ||
... | ||
// Runtime Use | ||
const main_cmd = try setup_cmd.init(alloc); | ||
defer main_cmd.deinit(); | ||
cova.parseArgs(..., main_cmd, ...); | ||
utils.displayCmdInfo(CustomCommand, main_cmd, alloc, stdout); | ||
cova.parseArgs(..., main_cmd, ...); | ||
utils.displayCmdInfo(CustomCommand, main_cmd, alloc, stdout); | ||
} | ||
``` |
Oops, something went wrong.