/ src / cli / commandParser.js
commandParser.js
 1  import { showErrorMessage } from "../utils/messages.js";
 2  
 3  export function handleCommandLineOperation() {
 4    return process.argv.length > 2;
 5  }
 6  
 7  export function parseCommandLineArgs() {
 8    const args = process.argv.slice(2);
 9    if (args.length === 0) return null;
10  
11    switch (args[0]) {
12      case "--upload":
13        if (args.length !== 2) {
14          console.log(
15            showErrorMessage("Usage: npx codexstorage --upload <filename>"),
16          );
17          process.exit(1);
18        }
19        return { command: "upload", value: args[1] };
20  
21      case "--download":
22        if (args.length !== 2) {
23          console.log(
24            showErrorMessage("Usage: npx codexstorage --download <cid>"),
25          );
26          process.exit(1);
27        }
28        return { command: "download", value: args[1] };
29  
30      default:
31        console.log(
32          showErrorMessage(
33            "Invalid command. Available commands:\n\n" +
34              "npx codexstorage\n" +
35              "npx codexstorage --upload <filename>\n" +
36              "npx codexstorage --download <cid>",
37          ),
38        );
39        process.exit(1);
40    }
41  }