Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,25 @@ In other files:
#include "optly.h"
#include <stdio.h>

static OptlyCommand cmd = {
.name = "app",

.flags = optly_flags(
optly_flag_bool("verbose", 'v', "Enable verbose output", .value.as_bool = false),
optly_flag_uint32("threads", 't', "Worker threads", .value.as_uint32 = 4)
),

.commands = optly_commands(
optly_command("run", "Run server",
.flags = optly_flags(
optly_flag_uint16("port", 'p', "Server port", .value.as_uint16 = 8080)
int main(int argc, char **argv)
{
OptlyCommand cmd = {
.name = "app",

.flags = optly_flags(
optly_flag_bool("verbose", 'v', "Enable verbose output", .value.as_bool = false),
optly_flag_uint32("threads", 't', "Worker threads", .value.as_uint32 = 4)
),

.commands = optly_commands(
optly_command("run", "Run server",
.flags = optly_flags(
optly_flag_uint16("port", 'p', "Server port", .value.as_uint16 = 8080)
)
)
)
)
};
};

int main(int argc, char **argv)
{
optly_parse_args(argc, argv, &cmd);

printf("threads: %u\n", optly_uint32(&cmd, "threads"));
Expand Down
4 changes: 1 addition & 3 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,10 @@ for example in $examples; do
fi

if $COMPILE; then
$CMD &
$CMD
fi
done

wait

if $VERBOSE; then
set -x
fi
Expand Down
26 changes: 13 additions & 13 deletions examples/enum_flag.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
#define OPTLY_IMPLEMENTATION
#include "optly.h"

static OptlyCommand cmd = {
.name = NULL,
.description = NULL,
int main(int argc, char **argv) {
OptlyCommand cmd = {
.name = NULL,
.description = NULL,

.flags = optly_flags(
// First value in array is default value, the rest of them are possible values.
// You need to manually cast to (char *[]) beaause it is C :(
// Also don't forget to close the values array with NULL
optly_flag_enum("level", 'l', .value.as_enum = (char *[]){"info", "verb", "info", "warn", NULL}),
.flags = optly_flags(
// First value in array is default value, the rest of them are possible values.
// You need to manually cast to (char *[]) beaause it is C :(
// Also don't forget to close the values array with NULL
optly_flag_enum("level", 'l', .value.as_enum = (char *[]){"info", "verb", "info", "warn", NULL}),

// You can ues `optly_enum_values` to not manually cast into char*[] and apeend NULL to the end
optly_flag_enum("enum", 'e', optly_enum_values(NULL, "a", "b", "c"), .required = true)
),
};
// You can use `optly_enum_values` to not manually cast into char*[] and apeend NULL to the end
optly_flag_enum("enum", 'e', optly_enum_values(NULL, "a", "b", "c"), .required = true)
),
};

int main(int argc, char **argv) {
optly_parse_args(argc, argv, &cmd);

// You can get the value with handy accessor function, as always
Expand Down
139 changes: 70 additions & 69 deletions examples/from_doc.c
Original file line number Diff line number Diff line change
@@ -1,77 +1,78 @@
#define OPTLY_IMPLEMENTATION
#include "optly.h"

// You need to define "main" command, which is your application.
static OptlyCommand cmd = {

// Name will be used in usage. If you set is as NULL it will be argv[0]
.name = NULL,

// If not null, then it will be used in usage
.description = NULL,

// This is your "top-level global" flags. They should be specified before
// any command (./app --thread 2 -v)
.flags =
(OptlyFlag[]){
// Long (full name) short name description (for usage) Default
// value Flag value type
{"verbose", 'v', "Enable verbose output", .value.as_bool = false, .type = OPTLY_TYPE_BOOL},

// Values are unions, so you need to specify member of value with
// correct type some way
{"threads", 't', "Worker threads", false, false, {.as_uint32 = 4}, OPTLY_TYPE_UINT32},

// Flag arrays should alwasy ends with NULL_FLAG. Try to not forget
// about it :)
NULL_FLAG,
},

// This is your commands. (git `commit`, docker `compose` `up`)
.commands = (OptlyCommand[]){

// Instead of defining whole struct manually you can use helper
// functions
optly_command(
"run", "Runs server",

// optly_flags macro deals with type castings and closing array with
// NULL_FLAG
optly_flags(

// This is *command* flag. `./app -p 8080 run` will not work,
// but `./app run -p 8080` will
optly_flag_uint16("port", 'p', "Server port", .value.as_uint16 = 8080),

// NOTE: this flag and 'verbose' GLOBAL flag are not the same
// `./app -v run` - enables global -v flag
// `./app run -v` - enables command -v flag
optly_flag_bool("verbose", 'v', "Enable verbose output for worker", .value.as_bool = false)
),

// This is subcommands of command "run"
optly_commands(

// `./app run check` subcommand have no description, flags or
// subcommands.
// NULL is here to suppress warning about not providing
// argument in variadic macro
optly_command("check", NULL),

// `./app run dump_config` subcommands
optly_command("dump_config",

// We skipped description, so we need to specify fileds now
.flags = optly_flags(optly_flag_bool("color", 'c', "Show colors", .value.as_bool = false)))
int main(int argc, char **argv) {
// You need to define "main" command, which is your application.
OptlyCommand cmd = {

// Name will be used in usage. If you set is as NULL it will be argv[0]
.name = NULL,

// If not null, then it will be used in usage
.description = NULL,

// This is your "top-level global" flags. They should be specified before
// any command (./app --thread 2 -v)
.flags =
(OptlyFlag[]){
// Long (full name) short name description (for usage) Default
// value Flag value type
{"verbose", 'v', "Enable verbose output", .value.as_bool = false, .type = OPTLY_TYPE_BOOL},

// Values are unions, so you need to specify member of value with
// correct type some way
{"threads", 't', "Worker threads", false, false, {.as_uint32 = 4}, OPTLY_TYPE_UINT32},

// Flag arrays should alwasy ends with NULL_FLAG. Try to not forget
// about it :)
NULL_FLAG,
},

// This is your commands. (git `commit`, docker `compose` `up`)
.commands = (OptlyCommand[]){

// Instead of defining whole struct manually you can use helper
// functions
optly_command(
"run",
"Runs server",

// optly_flags macro deals with type castings and closing array with
// NULL_FLAG
optly_flags(

// This is *command* flag. `./app -p 8080 run` will not work,
// but `./app run -p 8080` will
optly_flag_uint16("port", 'p', "Server port", .value.as_uint16 = 8080),

// NOTE: this flag and 'verbose' GLOBAL flag are not the same
// `./app -v run` - enables global -v flag
// `./app run -v` - enables command -v flag
optly_flag_bool("verbose", 'v', "Enable verbose output for worker", .value.as_bool = false)
),

// This is subcommands of command "run"
optly_commands(

// `./app run check` subcommand have no description, flags or
// subcommands.
// NULL is here to suppress warning about not providing
// argument in variadic macro
optly_command("check", NULL),

// `./app run dump_config` subcommands
optly_command("dump_config",

// We skipped description, so we need to specify fileds now
.flags = optly_flags(optly_flag_bool("color", 'c', "Show colors", .value.as_bool = false)))
),

// Positioanl arguments can be defined lilke this
optly_positionals(optly_positional("address", "Address to listen on", .min = 0, .max = 1)),
),
}
};

// Positioanl arguments can be defined lilke this
optly_positionals(optly_positional("address", "Address to listen on", .min = 0, .max = 1)),
),
}
};

int main(int argc, char **argv) {
optly_parse_args(argc, argv, &cmd);

printf("Verbose: %s\n", optly_flag_value_bool(&cmd, "verbose") ? "true" : "false");
Expand Down
Loading
Loading