Program no longer has subcommands
This commit is contained in:
parent
ec7cfb4448
commit
dddb8648f4
100
cmd/bulb/main.ha
100
cmd/bulb/main.ha
@ -19,71 +19,63 @@ export fn main() void = {
|
|||||||
const cmd = getopt::parse(
|
const cmd = getopt::parse(
|
||||||
os::args,
|
os::args,
|
||||||
"bulletin board",
|
"bulletin board",
|
||||||
("post", [
|
('b', "board", "specify a board other than general"),
|
||||||
('a', "post anonymously"),
|
('l', "list available boards and exit"),
|
||||||
('b', "board", "post on a board other than general"),
|
('n', "number", "display N most recent messages"),
|
||||||
]): getopt::subcmd_help,
|
('p', "post a message (from stdin)"),
|
||||||
("read", [
|
('u', "operate undercover (anonymously)"));
|
||||||
('b', "board", "read a board other than general"),
|
|
||||||
('n', "number", "display N most recent messages"),
|
|
||||||
]): getopt::subcmd_help);
|
|
||||||
defer getopt::finish(&cmd);
|
defer getopt::finish(&cmd);
|
||||||
|
|
||||||
match (cmd.subcmd) {
|
|
||||||
case let subcmd: (str, *getopt::command) =>
|
|
||||||
if (subcmd.0 == "post") {
|
|
||||||
subcmd_post(subcmd.1);
|
|
||||||
} else if (subcmd.0 == "read") {
|
|
||||||
match (subcmd_read(subcmd.1)) {
|
|
||||||
case errors::invalid =>
|
|
||||||
getopt::printhelp(os::stdout, name, cmd.help)!;
|
|
||||||
os::exit(os::status::FAILURE);
|
|
||||||
case void => void;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
case void =>
|
|
||||||
subcmd_ls(&cmd);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
fn subcmd_post(cmd: *getopt::command) void = {
|
|
||||||
let anonymous = false;
|
|
||||||
let board = default_board;
|
let board = default_board;
|
||||||
for (let opt .. cmd.opts) {
|
let list = false;
|
||||||
switch (opt.0) {
|
let number = 8;
|
||||||
case 'a' => anonymous = true;
|
let post = false;
|
||||||
case 'b' => board = opt.1;
|
let anonymous = false;
|
||||||
case => abort();
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
let user_name = if (anonymous) void else get_user_name();
|
|
||||||
let message = strings::join(" ", cmd.args...);
|
|
||||||
defer free(message);
|
|
||||||
|
|
||||||
match (bulb::post(os::stdin, board, user_name)) {
|
|
||||||
case let err: bulb::error =>
|
|
||||||
fmt::errorf("{}: Could not read {}: {}\n", name, board, bulb::strerror(err))!;
|
|
||||||
case void => void;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
fn subcmd_read(cmd: *getopt::command) (void | errors::invalid) = {
|
|
||||||
let board = default_board;
|
|
||||||
let number = 8;
|
|
||||||
for (let opt .. cmd.opts) {
|
for (let opt .. cmd.opts) {
|
||||||
switch (opt.0) {
|
switch (opt.0) {
|
||||||
case 'b' =>
|
case 'b' =>
|
||||||
board = opt.1;
|
board = opt.1;
|
||||||
|
case 'l' =>
|
||||||
|
list = true;
|
||||||
case 'n' =>
|
case 'n' =>
|
||||||
match (strconv::stoi(opt.1)) {
|
match (strconv::stoi(opt.1)) {
|
||||||
case let num: int => number = num;
|
case let num: int =>
|
||||||
case => return errors::invalid;
|
number = num;
|
||||||
|
case =>
|
||||||
|
getopt::printhelp(os::stdout, name, cmd.help)!;
|
||||||
|
os::exit(os::status::FAILURE);
|
||||||
};
|
};
|
||||||
|
case 'p' =>
|
||||||
|
post = true;
|
||||||
|
case 'u' =>
|
||||||
|
anonymous = true;
|
||||||
case => abort();
|
case => abort();
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (list) {
|
||||||
|
// list boards and exit
|
||||||
|
match (bulb::list(os::stdout)) {
|
||||||
|
case let err: bulb::error =>
|
||||||
|
fmt::errorf("{}: Could not list boards: {}\n", name, bulb::strerror(err))!;
|
||||||
|
os::exit(os::status::FAILURE);
|
||||||
|
case void =>
|
||||||
|
os::exit(os::status::SUCCESS);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
if (post) {
|
||||||
|
// post a message (from stdin)
|
||||||
|
let user_name = if (anonymous) void else get_user_name();
|
||||||
|
match (bulb::post(os::stdin, board, user_name)) {
|
||||||
|
case let err: bulb::error =>
|
||||||
|
fmt::errorf("{}: Could not post to {}: {}\n", name, board, bulb::strerror(err))!;
|
||||||
|
case void => void;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
// read latest posts on the board
|
||||||
match (bulb::read(os::stdout, board, number)) {
|
match (bulb::read(os::stdout, board, number)) {
|
||||||
case let err: bulb::error =>
|
case let err: bulb::error =>
|
||||||
fmt::errorf("{}: Could not read {}: {}\n", name, board, bulb::strerror(err))!;
|
fmt::errorf("{}: Could not read {}: {}\n", name, board, bulb::strerror(err))!;
|
||||||
@ -91,14 +83,6 @@ fn subcmd_read(cmd: *getopt::command) (void | errors::invalid) = {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
fn subcmd_ls(cmd: *getopt::command) void = {
|
|
||||||
match (bulb::list(os::stdout)) {
|
|
||||||
case let err: bulb::error =>
|
|
||||||
fmt::errorf("{}: Could not list boards: {}\n", name, bulb::strerror(err))!;
|
|
||||||
case void => void;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
fn get_user_name() str = {
|
fn get_user_name() str = {
|
||||||
// FIXME: could not find support for getting a username from a uid in
|
// FIXME: could not find support for getting a username from a uid in
|
||||||
// hare. when it is added, use that here.
|
// hare. when it is added, use that here.
|
||||||
|
Loading…
Reference in New Issue
Block a user