|
@@ -8,15 +8,7 @@ let err i msgs =
|
|
|
msgs |> List.cons exe |> String.concat ": " |> prerr_endline;
|
|
|
i
|
|
|
|
|
|
-let print_tag = function Name.Tag tv -> Printf.printf "%s\n" tv
|
|
|
-
|
|
|
-let tag_lst files =
|
|
|
- files
|
|
|
- |> List.map (fun f -> (Name.parse f).tags)
|
|
|
- |> List.concat |> List.iter print_tag;
|
|
|
- 0
|
|
|
-
|
|
|
-let file_rename f p' =
|
|
|
+let file_rename oc f p' =
|
|
|
let f' = Name.unparse p' in
|
|
|
(* https://ocaml.github.io/ocamlunix/ocamlunix.html#sec13 *)
|
|
|
match Sys.file_exists f' with
|
|
@@ -24,66 +16,69 @@ let file_rename f p' =
|
|
|
| false -> (
|
|
|
try
|
|
|
Sys.rename f f';
|
|
|
- Printf.printf "%s --> %s\n" f f';
|
|
|
+ Printf.fprintf oc "%s --> %s\n" f f';
|
|
|
0
|
|
|
- with Sys_error e -> err 4 [ f; e ] )
|
|
|
+ with Sys_error e -> err 4 [ f; e ])
|
|
|
|
|
|
-let tag_add tag f =
|
|
|
+let tag_add oc tag f =
|
|
|
let p = Name.parse f in
|
|
|
let tags = p.tags |> List.cons tag |> List.sort_uniq compare in
|
|
|
- { p with tags } |> file_rename f
|
|
|
+ { p with tags } |> file_rename oc f
|
|
|
|
|
|
-let tag_del tag f =
|
|
|
+let tag_del oc tag f =
|
|
|
let p = Name.parse f in
|
|
|
let tags =
|
|
|
p.tags |> List.sort_uniq compare |> List.filter (fun x -> tag <> x)
|
|
|
in
|
|
|
- { p with tags } |> file_rename f
|
|
|
-
|
|
|
-let print_title = function Name.Title tv -> Printf.printf "%s\n" tv
|
|
|
+ { p with tags } |> file_rename oc f
|
|
|
|
|
|
-let title_get files =
|
|
|
+let title_get oc files =
|
|
|
+ let print_title = function Name.Title tv -> Printf.fprintf oc "%s\n" tv in
|
|
|
files |> List.map (fun f -> (Name.parse f).title) |> List.iter print_title;
|
|
|
0
|
|
|
|
|
|
-let title_set title f = { (Name.parse f) with title } |> file_rename f
|
|
|
-
|
|
|
-let each v fkt lst =
|
|
|
- lst |> List.iter (fun x -> ignore (fkt v x));
|
|
|
- 0
|
|
|
-
|
|
|
-let print_version () =
|
|
|
- let exe = Filename.basename Sys.executable_name in
|
|
|
- Printf.printf "%s: https://mro.name/%s/v%s, built: %s\n" exe "meta"
|
|
|
- Version.git_sha Version.date;
|
|
|
- 0
|
|
|
-
|
|
|
-let print_help () =
|
|
|
- let msg =
|
|
|
- "Add, delete and list tags from filenames, inspired by \
|
|
|
- https://karl-voit.at/managing-digital-photographs/\n\n\
|
|
|
- SYNOPSIS\n\n\
|
|
|
- \ $ meta -h # get this help\n\
|
|
|
- \ $ meta -v # version\n\n\
|
|
|
- \ $ meta tag lst [file]\n\
|
|
|
- \ $ meta tag add <tag> [file]\n\
|
|
|
- \ $ meta tag del <tag> [file]\n\n\
|
|
|
- \ $ meta title get [file]\n\
|
|
|
- \ $ meta title set <title> [file]\n\n\
|
|
|
- EXAMPLE\n\n\
|
|
|
- \ $ meta tag lst * | sort | uniq -c\n"
|
|
|
- in
|
|
|
- Printf.printf "%s\n" msg;
|
|
|
- 0
|
|
|
+let title_set oc title f = { (Name.parse f) with title } |> file_rename oc f
|
|
|
|
|
|
let () =
|
|
|
- ( match Sys.argv |> Array.to_list |> List.tl with
|
|
|
- | [ "-h" ] | [ "--help" ] -> print_help ()
|
|
|
- | [ "-v" ] | [ "--version" ] -> print_version ()
|
|
|
- | "tag" :: "lst" :: fs -> fs |> tag_lst
|
|
|
- | "tag" :: "add" :: t :: fs -> fs |> each (Name.Tag t) tag_add
|
|
|
- | "tag" :: "del" :: t :: fs -> fs |> each (Name.Tag t) tag_del
|
|
|
- | "title" :: "get" :: fs -> fs |> title_get
|
|
|
- | "title" :: "set" :: t :: fs -> fs |> each (Name.Title t) title_set
|
|
|
- | _ -> err 2 [ "get help with -h" ] )
|
|
|
+ let print_help oc =
|
|
|
+ let msg =
|
|
|
+ "Add, delete and list tags from filenames, inspired by \
|
|
|
+ https://karl-voit.at/managing-digital-photographs/\n\n\
|
|
|
+ SYNOPSIS\n\n\
|
|
|
+ \ $ meta -h # get this help\n\
|
|
|
+ \ $ meta -V # version\n\n\
|
|
|
+ \ $ meta tag lst [file]\n\
|
|
|
+ \ $ meta tag add <tag> [file]\n\
|
|
|
+ \ $ meta tag del <tag> [file]\n\n\
|
|
|
+ \ $ meta title get [file]\n\
|
|
|
+ \ $ meta title set <title> [file]\n\n\
|
|
|
+ EXAMPLE\n\n\
|
|
|
+ \ $ meta tag lst * | sort | uniq -c\n"
|
|
|
+ in
|
|
|
+ Printf.fprintf oc "%s\n" msg;
|
|
|
+ 0
|
|
|
+ and print_version oc =
|
|
|
+ let exe = Filename.basename Sys.executable_name in
|
|
|
+ Printf.fprintf oc "%s: https://mro.name/%s/v%s, built: %s\n" exe "meta"
|
|
|
+ Version.git_sha Version.date;
|
|
|
+ 0
|
|
|
+ and tag_lst oc files =
|
|
|
+ let print_tag = function Name.Tag tv -> Printf.fprintf oc "%s\n" tv in
|
|
|
+ files
|
|
|
+ |> List.map (fun f -> (Name.parse f).tags)
|
|
|
+ |> List.concat |> List.iter print_tag;
|
|
|
+ 0
|
|
|
+ and each v fkt lst =
|
|
|
+ lst |> List.iter (fun x -> ignore (fkt v x));
|
|
|
+ 0
|
|
|
+ in
|
|
|
+ (match Sys.argv |> Array.to_list |> List.tl with
|
|
|
+ | [ "-h" ] | [ "--help" ] -> print_help stdout
|
|
|
+ | [ "-V" ] | [ "--version" ] -> print_version stdout
|
|
|
+ | "tag" :: "lst" :: fs -> fs |> tag_lst stdout
|
|
|
+ | "tag" :: "add" :: t :: fs -> fs |> each (Name.Tag t) (tag_add stdout)
|
|
|
+ | "tag" :: "del" :: t :: fs -> fs |> each (Name.Tag t) (tag_del stdout)
|
|
|
+ | "title" :: "get" :: fs -> fs |> title_get stdout
|
|
|
+ | "title" :: "set" :: t :: fs -> fs |> each (Name.Title t) (title_set stdout)
|
|
|
+ | _ -> err 2 [ "get help with -h" ])
|
|
|
|> exit
|