Skip to content

Instantly share code, notes, and snippets.

@Artiavis
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Artiavis/57798118f5a6a1c8e22e to your computer and use it in GitHub Desktop.
Save Artiavis/57798118f5a6a1c8e22e to your computer and use it in GitHub Desktop.
An adaptation of my rmtex.py script, but for F#. CLI based on the tutorial by "F Sharp for Fun and Profit".
open System
open System.IO
type CommandLineOptions = {
verbose: bool;
recrsv: bool;
exts: list<string>;
}
let rec parseCommandLine args optionsSoFar =
match args with
// empty list means we're done
| [] -> optionsSoFar
// match verbose flag
| "/v"::xs ->
let newOptionsSoFar = {optionsSoFar with verbose=true}
parseCommandLine xs newOptionsSoFar
// match recursive flag
| "/R"::xs ->
let newOptionsSoFar = {optionsSoFar with recrsv=true}
parseCommandLine xs newOptionsSoFar
// match set of extensions
| "/E"::xs ->
match xs with
| [] -> optionsSoFar
| (ext:string)::xss ->
let newOptionsSoFar = {optionsSoFar with exts=(ext::optionsSoFar.exts)}
parseCommandLine xss newOptionsSoFar
// handle unrecognized param and keep going
| x::xs ->
eprintf "Option '%s' is not recognized" x
parseCommandLine xs optionsSoFar
let defaultOptions = {
verbose = false;
recrsv = false;
exts = [];
}
let default_exts = Set.ofList [ ".toc"; ".bbl"; ".aux"; ".gz";
".blg"; ".dvi"; ".log"; ".out" ]
let ends_with_ext (exts:Set<string>) (path:string) =
let file_ext = Path.GetExtension path
Set.contains file_ext exts
let remove_file (exts:Set<string>) (file_path:string) =
match ends_with_ext exts file_path with
| true ->
File.Delete file_path
| false -> ()
let rec remove_files_rec (rec_opt:bool) (exts:Set<string>) (folder_path:string) =
Directory.GetFiles folder_path
|> Array.filter (ends_with_ext exts)
|> Array.iter (remove_file exts)
match rec_opt with
| false -> ()
| true -> Directory.GetDirectories folder_path
|> Array.iter (remove_files_rec true exts)
/// Bind false to remove_files_recursively
let remove_files = remove_files_rec false
// flags
// /R for recursive
// /E for specifying a list of extensions
[<EntryPoint>]
let main argv =
let parsed_opts = parseCommandLine (List.ofArray argv) defaultOptions
let is_verbose = parsed_opts.verbose
let exts = match parsed_opts.exts with
| [] -> default_exts
| (exts:list<string>) -> Set.ofList exts
match is_verbose with
| true -> eprintfn "%O" exts
| false -> ()
match parsed_opts.recrsv with
| true -> remove_files_rec true exts Environment.CurrentDirectory
| false -> remove_files exts Environment.CurrentDirectory
let v = Console.ReadLine
0 // return an integer exit code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment