nsub: use -o for output
authorNiki Roo <niki@nikiroo.be>
Thu, 26 May 2022 11:31:42 +0000 (13:31 +0200)
committerNiki Roo <niki@nikiroo.be>
Thu, 26 May 2022 11:31:42 +0000 (13:31 +0200)
README-fr.md
README.md
src/nsub/nsub_main.c

index 4896417f2033b6dd7439656a8736f82475de5c47..16135814746805ff2c5a25051db681212363277c 100644 (file)
@@ -7,8 +7,8 @@ Converti entre les formats Subtitle/Lyrics (webvtt/srt/lrc).
 ## Synopsis
 
 - `nsub --help`
-- `nsub` (`--from FMT`) (`--to FMT`) (`--apply-offset`) (`IN` (`OUT`))
-- `nsub` (`-f FMT`) (`-t FMT`) (`-o`) (`IN` (`OUT`))
+- `nsub` (`--from FMT`) (`--to FMT`) (`--apply-offset`) (--output `OUT`) (`IN`)
+- `nsub` (`-f FMT`) (`-t FMT`) (`-a`) (`-o OUT`) (`IN`)
 
 ## Description
 
@@ -27,12 +27,12 @@ Il ne nécessite pas de librairies externes.
 
 ## Options
 
-- **--help** (or **-h**) : information sur la syntaxe du programme
-- **--from** (or **-f**) **FMT** : choisi le format d'entrée
-- **--to** (or **-t**) **FMT** : choisi le format de sortie
-- **--apply-offset** (or **-o**) : applique l'offset interne au fichier dans les calcul de temps des paroles
+- **--help** (ou **-h**) : information sur la syntaxe du programme
+- **--from** (ou **-f**) **FMT** : choisi le format d'entrée
+- **--to** (ou **-t**) **FMT** : choisi le format de sortie
+- **--apply-offset** (ou **-a**) : applique l'offset interne au fichier dans les calcul de temps des paroles
+- **--output** (ou **-o**) **OUT**: le fichier destination ou '-' pour stdout (défaut)
 - **IN** : le fichier source ou '-' pour stdin (défaut)
-- **OUT** : le fichier destination ou '-' pour stdout (défaut)
 
 Note : les formats in/out seront devinés en fonction de l'extension si nécessaire/possible
 Note : pour spécifier un fichier appelé tiret (-), préfixez-le avec un chemin (ex : './-')
index 1468cd7fa5421df5d04cc3cf23b1ebda5e81a04c..1efdcdf1d0f23e41b4224ab7d54f6a455b4fd3d7 100644 (file)
--- a/README.md
+++ b/README.md
@@ -7,8 +7,8 @@ Converts between Subtitle/Lyrics formats (webvtt/srt/lrc).
 ## Synopsis
 
 - `nsub --help`
-- `nsub` (`--from FMT`) (`--to FMT`) (`--apply-offset`) (`IN` (`OUT`))
-- `nsub` (`-f FMT`) (`-t FMT`) (`-o`) (`IN` (`OUT`))
+- `nsub` (`--from FMT`) (`--to FMT`) (`--apply-offset`) (--output `OUT`) (`IN`)
+- `nsub` (`-f FMT`) (`-t FMT`) (`-a`) (`-o OUT`) (`IN`)
 
 ## Description
 
@@ -30,9 +30,9 @@ It does not require external libraries.
 - **--help** (or **-h**): information about the syntax
 - **--from** (or **-f**) **FMT**: select the input format FMT
 - **--to** (or **-t**) **FMT**: select the output format FMT
-- **--apply-offset** (or **-o**): apply the offset tag value to the lyrics
+- **--apply-offset** (or **-a**): apply the offset tag value to the lyrics
+- **--output** (or **-o**) **OUT**: the output file or '-' for stdout (which is the default)
 - **IN**: the input file or '-' for stdin (which is the default)
-- **OUT**: the output file or '-' for stdout (which is the default)
 
 Note: the in/out formats will be guessed from the extension if needed/possible
 Note: to specify a file named dash (-), prefix it with a path (e.g., './-')
index 630605d8000a580fb2781791b6bd9909689a9935..27d4be5528f81bbefe96ebca45d637cc0215e3d2 100644 (file)
@@ -36,6 +36,11 @@ int main(int argc, char **argv) {
        char *out_file = NULL;
        int apply_offset = 0;
 
+       if (argc <= 1) {
+               help(argv[0]);
+               return 5;
+       }
+
        for (int i = 1; i < argc; i++) {
                char *arg = argv[i];
                if (!strcmp("--help", arg) || !strcmp("-h", arg)) {
@@ -62,8 +67,22 @@ int main(int argc, char **argv) {
                                fprintf(stderr, "Unsupported output format: %s\n", argv[i]);
                                return 9;
                        }
-               } else if (!strcmp("--apply-offset", arg) || !strcmp("-o", arg)) {
+               } else if (!strcmp("--apply-offset", arg) || !strcmp("-a", arg)) {
                        apply_offset = 1;
+               } else if (!strcmp("--output", arg) || !strcmp("-o", arg)) {
+                       if (i + 1 >= argc) {
+                               fprintf(stderr,
+                                               "The parameter --output/-o requires an argument\n");
+                               return 5;
+                       }
+                       out_file = argv[++i];
+                       if (to == NSUB_FMT_UNKNOWN) {
+                               char *ext = strrchr(arg, '.');
+                               if (ext) {
+                                       ext++;
+                                       to = nsub_parse_fmt(ext, 0);
+                               }
+                       }
                } else if (!in_file) {
                        in_file = arg;
                        if (from == NSUB_FMT_UNKNOWN) {
@@ -160,14 +179,13 @@ NSUB_FORMAT nsub_parse_fmt(char *type, int required) {
 void help(char *program) {
        printf("NSub subtitles conversion program\n");
        printf("Syntax:\n");
-       printf(
-                       "\t%s (--from FMT) (--to FMT) (--apply-offset) (IN_FILE (OUT_FILE))\n",
-                       program);
+       printf("\t%s (--from FMT) (--to FMT) (--apply-offset)\n"
+                       "\t\t (--output OUT_FILE) (IN_FILE)\n", program);
        printf("\t> --help (or -h): this help message\n");
        printf("\t> --from (or -f) FMT: select the input format FMT\n");
        printf("\t> --to (or -t) FMT: select the output format FMT\n");
        printf(
-                       "\t> --apply-offset (or -o): apply the offset tag value to the lyrics\n");
+                       "\t> --apply-offset (or -a): apply the offset tag value to the lyrics\n");
        printf(
                        "\t> IN_FILE: the input file or '-' for stdin (which is the default)\n");
        printf(