aboutsummaryrefslogtreecommitdiff
path: root/src/args.c
blob: 67ecf9f724e2e99cdc27612a02a6162ce49a93d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "args.h"
#include "log.h"

#include "stdbool.h"
#include "stdio.h"
#include "string.h"
#include "utils.h"

char *help = "▄▄▄▄▄▄▄▄▄▄▄     ▄\n"
             "█    █    █  ▄  █\n"
             "█▄▄▄▄█▄▄▄▄█▄▄█▄▄█\n"
             "█\n\n"
             "pow! - the pow!erful package manager\n"
             "Made with love by forever2k38\n"
             "Source code at: https://codeberg.org/einfach/pow\n"
             "Use outside of einfach is unsupported.\n"
             "This software comes with no warranty.\n"
             "\n"
             "Usage: pow\n"
             "  --help Prints this help message.\n"
             "  -c     Specify config file path.\n";

int pow_args_parse(int argc, char **argv, pow_args *args) {
  int flags = 0;

  bool next_flag_config = false;
  bool next_flag_package = false;

  for (int i = 0; i < argc; i++) {
    char *arg = argv[i];

    if (next_flag_config) {
      if (!strncmp(arg, "-", sizeof("-") - 1)) {
        pow_log_error("Missing argument for -c");

        return 1;
      }

      args->cfg_path = arg;

      next_flag_config = false;

      continue;
    }

    if (next_flag_package) {
      if (!strncmp(arg, "-", sizeof("-") - 1)) {
        pow_log_error("Missing package argument");

        return 1;
      }

      args->package = arg;

      next_flag_package = false;

      continue;
    }

    if (strcmp(arg, "--help") == 0) {
      flags |= POW_FLAG_HELP;

      continue;
    }

    if (strcmp(arg, "-c") == 0) {
      next_flag_config = true;

      continue;
    }

    if (string_has_prefix("-", arg)) {
      size_t len = strlen(arg);

      for (size_t j = 0; j < len; j++) {
        char c = (char)arg[j];

        switch (c) {
        case 'S': {
          if (flags & POW_FLAG_SYNC) {
            pow_log_info("flag double defined, skipping");
          }

          flags |= POW_FLAG_SYNC;
          next_flag_package = true;

          break;
        };
        case 'Q': {
          if (flags & POW_FLAG_QUERY) {
            pow_log_info("flag double defined, skipping");
          }

          flags |= POW_FLAG_QUERY;

          next_flag_package = true;

          break;
        }
        case 'I': {
          if (flags & POW_FLAG_INSTALL) {
            pow_log_info("flag double defined, skipping");
          }

          flags |= POW_FLAG_INSTALL;

          next_flag_package = true;

          break;
        }
        }
      }

      continue;
    }
  }

  if (next_flag_config) {
    pow_log_error("Missing argument for -c");
  }

  if (!args->cfg_path)
    args->cfg_path = DEFAULT_CONFIG_PATH;

  args->flags = flags;

  return 0;
}

void pow_args_print_help() { printf("%s", help); };