Skip to content

Commit

Permalink
Update version, add building instructions, and use clap for parsing a…
Browse files Browse the repository at this point in the history
…rguments
  • Loading branch information
metamethods committed Jan 3, 2024
1 parent 674f813 commit 33fe597
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 115 deletions.
218 changes: 217 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
[package]
name = "no"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
license = "GPL-3.0"
description = "Output a string repeatedly until killed."
homepage = "https://github.com/metamethods/no"
documentation = "https://github.com/metamethods/no/blob/master/README.md"
repository = "https://github.com/metamethods/no"
readme = "README.md"
keywords = ["unix", "no"]
authors = ["metamethods <[email protected]>"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.4.12", features = ["derive"] }
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ Repeatedly output a line with all specified STRING(s), or 'n'.

## Usage
```bash
no [STRING]
no [STRINGS]
```
```bash
no OPTIONS
```

## Building
### Prerequisites
* [Rustup](https://rustup.rs/)

### Instructions
1. Clone this repository via `git clone https://github.com/metamethods/no`
2. Build with `cargo` using `cargo build --release`
3. The binary will be located at `target/release/no`
4 changes: 0 additions & 4 deletions install-manpage.sh

This file was deleted.

45 changes: 0 additions & 45 deletions no.1

This file was deleted.

73 changes: 13 additions & 60 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,66 +1,19 @@
// This is the most shittiest code but it works lmao
use clap::Parser;

fn has_flag(flags: Vec<String>, arguments: &Vec<String>) -> bool {
return flags.iter().any(|flag| arguments.contains(flag));
}

fn get_flags(arguments: &Vec<String>) -> Vec<String> {
let mut flags: Vec<String> = Vec::new();
for argument in arguments {
if argument.starts_with("-") {
flags.push(argument.clone());
}
}
return flags;
/// Repeatedly output a line with all specified STRING(s), or 'n'.
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
strings: Option<Vec<String>>,
}

fn main() {
let arguments: Vec<String> = std::env::args().collect();

let help_flag: bool = has_flag(vec!["--help".to_string()], &arguments);
let version_flag: bool = has_flag(vec!["--version".to_string()], &arguments);

let flags = get_flags(&arguments);
let valid_flags = vec!["--help".to_string(), "--version".to_string()];

let expletive = arguments.get(1)
.unwrap_or(&"n".to_string()).clone();

for flag in flags {
if !valid_flags.contains(&flag) {
println!("no: invalid option -- '{}'", flag);
println!("Try 'no --help' for more information.");

std::process::exit(1);
}
}

if help_flag {
println!("Usage: no [STRING]...");
println!("or: no OPTION");
println!("Repeatedly output a line with all specified STRING(s), or 'n'.\n");
println!(" --help display this help and exit");
println!(" --version output version information and exit\n");
println!("GNU ballutils online help: <doesn't exist :troll:>");
println!("Report any translation bugs to <nope>");
println!("Full documentation <no documents to ever be made>");
println!("or available locally via: info '(ballutils) no invocation'");

std::process::exit(0);
}

if version_flag {
println!("no (GNU ballutils) 1.0");
println!("Copyright (C) 2024 Free Software Foundation, Inc.");
println!("License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.");
println!("This is free software: you are free to change and redistribute it.");
println!("There is NO WARRANTY, to the extent permitted by law.\n");
println!("Written by metamethods.");

std::process::exit(0);
}

loop {
println!("{expletive}");
println!(
"{}",
Args::parse().strings
.unwrap_or(vec!["n".to_string()])
.join(" ")
);
}
}
}

0 comments on commit 33fe597

Please sign in to comment.