Skip to content

Commit

Permalink
Adjsuting readme with more examples and links
Browse files Browse the repository at this point in the history
  • Loading branch information
BitlyTwiser committed Sep 23, 2024
1 parent e24bd07 commit d6ad626
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Snek builds dynamic (yet simple) CLI's using zigs meta programming to infer the

When the user goes to interact with the application, they can now utilize the flags you have established to run specific commands.

See the [completed](#example---full-execution) and functional example below or checkout the [main file](./src/main.zig) in the repo for a full working example as well.

#### Items to note:
1. If the user does not supply a value and the field is *not* otional, that is a failure case and a message is displayed to the user
2. If there is a default value on the field of the struct and a vale is not passed for that field, it is treated as an *optional* case and will use the static value (i.e. no error message and value is set)
Expand Down Expand Up @@ -78,7 +80,7 @@ When the user goes to interact with the application, they can now utilize the fl
#### Examples

Using the above struct as a reference, here are a few examples of calling the CLI:
##### Help
##### Example - Help Command
```
./<yourappname> -help
Expand All @@ -89,12 +91,12 @@ Using the above struct as a reference, here are a few examples of calling the C

Note: As you can see, the optionals are just that, *optional*. They are not required by your users and can be checked in the calling code in the standard ways that Zig handles optionals.
This is a design decisions allowing flexibility over the CLI to not lock users into using every flag etc..
##### Optionals
##### Example - Optionals
````
./<yourappname> -bool_test=true -word="I am a word!"
````

##### Defaults:
##### Example - Defaults:
```
./<yourappname> -bool_test=true -word="I am a word!"
Expand All @@ -104,6 +106,37 @@ This is a design decisions allowing flexibility over the CLI to not lock users i
./<yourappname> -bool_test=true -word="I am a word!" -test_defaults="I am a different word!"
```


##### Example - Full execution
```
const std = @import("std");
const snek = @import("lib.zig").Snek;
// Binary is also compiled for showcasing how to use the API
const T = struct {
name: []const u8,
location: u32,
exists: bool,
necessary: ?bool,
filled_optional: ?[]const u8,
default_name: []const u8 = "test default name",
};
// Example command after compilation:
// ./zig-out/bin/snek -name="test mctest" -location=420 -exists=true
pub fn main() !void {
var cli = try snek(T).init(std.heap.page_allocator);
const parsed_cli = try cli.parse();
// Necessary is skipped here to showcase optional values being ignored
std.debug.print("Name: {s}\n Location: {d}\n Exists: {any}\n Defualt value: {s}\n Filled Optional: {s}\n", .{ parsed_cli.name, parsed_cli.location, parsed_cli.exists, parsed_cli.default_name, parsed_cli.filled_optional orelse "badvalue" });
}
```
Compile with `zig build` then run the cli command:
```
./zig-out/bin/snek -name="test mctest" -location=420 -exists=true
```

#### Optionals
Using zig optionals, you can set selected flags to be ignored on the CLI, thus giving flexibilitiy on the behalf of the CLI creator to use or not use selected flags at their whimsy

Expand Down

0 comments on commit d6ad626

Please sign in to comment.