added usage information

This commit is contained in:
Valerie Wolfe 2024-03-11 22:57:45 -04:00
parent 6e31e23001
commit 7d42eaf2b8
3 changed files with 66 additions and 2 deletions

31
src/Info.cs Normal file
View file

@ -0,0 +1,31 @@
using System;
using System.IO;
public static class Globals {
public const string VERSION = "0.1.0";
public const string DEFAULT_PATH = "/usr/share/fortune-cs/";
}
public static class Usage {
public static void HelpText() {
VersionText();
Console.WriteLine(@"Valerie Wolfe <sleeplessval@gmail.com>
Shows quotes from a set of files.
usage: fortune-cs [files...]");
}
public static void ListText(string[] files) {
foreach(var file in files) {
var fileName = Path.GetFileNameWithoutExtension(file);
Console.WriteLine(fileName);
}
}
public static void VersionText() {
Console.WriteLine("fortune-cs v" + Globals.VERSION);
}
}

View file

@ -2,16 +2,32 @@ using System;
using System.IO; using System.IO;
using System.Security.Cryptography; using System.Security.Cryptography;
// handle help and version flags
if(Utilities.HasFlags(args, "-h", "--help")) {
Usage.HelpText();
return 0;
}
if(Utilities.HasFlags(args, "-v", "--version")) {
Usage.VersionText();
return 0;
}
// make sure fortune directory exists // make sure fortune directory exists
var resourcePath = "/usr/share/fortune-cs/"; var resourcePath = Globals.DEFAULT_PATH;
if(!Directory.Exists(resourcePath)) { if(!Directory.Exists(resourcePath)) {
Console.WriteLine("fortune-cs: directory '/usr/share/fortune-cs/' does not exist"); Console.WriteLine($"fortune-cs: directory '${resourcePath}' does not exist");
return 1; return 1;
} }
// pull file list // pull file list
var files = Directory.GetFiles(resourcePath, "*.txt"); var files = Directory.GetFiles(resourcePath, "*.txt");
// handle list flag
if(Utilities.HasFlags(args, "-l", "--list")) {
Usage.ListText(files);
return 0;
}
// choose a file and line // choose a file and line
var file = files[RandomNumberGenerator.GetInt32(files.Length)]; var file = files[RandomNumberGenerator.GetInt32(files.Length)];
var lines = File.ReadAllLines(file); var lines = File.ReadAllLines(file);

17
src/Util.cs Normal file
View file

@ -0,0 +1,17 @@
using System;
public static class Utilities {
/// <summary>
/// Checks if a given flag is set in `args`.
/// </summary>
public static bool HasFlags(string[] args, params string[] flags) {
foreach(string flag in flags)
// using `Array.IndexOf` since including Linq for `array.Contains` increases binary size
if(Array.IndexOf(args, flag) != -1)
return true;
return false;
}
}