WGet.NET 4.4.1
A WinGet wrapper library for .Net.
Loading...
Searching...
No Matches
WinGetArguments.cs
1//--------------------------------------------------//
2// Created by basicx-StrgV //
3// https://github.com/basicx-StrgV/ //
4//--------------------------------------------------//
5using System;
7
8namespace WGetNET
9{
17 {
22 internal enum WinGetAction
23 {
24 Unspecified,
26 Export,
27 Import,
28 Hash,
29 }
30
34 public string Arguments
35 {
36 get
37 {
38 return _arguments.Trim();
39 }
40 }
41
43 public bool IsEmpty
44 {
45 get
46 {
47 return string.IsNullOrWhiteSpace(_arguments);
48 }
49 }
50
51 private readonly WinGetAction _action;
52
53 private string _arguments;
54
66 internal WinGetArguments(string baseCmd, WinGetAction action = WinGetAction.Unspecified)
67 {
68 _arguments = baseCmd;
69 _action = action;
70 }
71
72 //---Base Cmd's--------------------------------------------------------------------------------
80 public static WinGetArguments WinGet()
81 {
82 return new WinGetArguments("");
83 }
84
91 public static WinGetArguments Settings()
92 {
93 return new WinGetArguments("settings");
94 }
95
103 {
104 return new WinGetArguments("settings export");
105 }
106
113 public static WinGetArguments List()
114 {
115 return new WinGetArguments("list");
116 }
117
124 public static WinGetArguments Search()
125 {
126 return new WinGetArguments("search");
127 }
128
135 public static WinGetArguments Install()
136 {
137 return new WinGetArguments("install");
138 }
139
146 public static WinGetArguments Upgrade()
147 {
148 return new WinGetArguments("upgrade");
149 }
150
158 {
159 return new WinGetArguments("uninstall");
160 }
161
168 public static WinGetArguments Download()
169 {
170 return new WinGetArguments("download", WinGetAction.Download);
171 }
172
179 public static WinGetArguments Repair()
180 {
181 return new WinGetArguments("repair");
182 }
183
190 public static WinGetArguments Export()
191 {
192 return new WinGetArguments("export", WinGetAction.Export);
193 }
194
201 public static WinGetArguments Import()
202 {
203 return new WinGetArguments("import", WinGetAction.Import);
204 }
205
212 public static WinGetArguments Hash()
213 {
214 return new WinGetArguments("hash", WinGetAction.Hash);
215 }
216
223 public static WinGetArguments PinList()
224 {
225 return new WinGetArguments("pin list");
226 }
227
234 public static WinGetArguments PinAdd()
235 {
236 return new WinGetArguments("pin add");
237 }
238
246 {
247 return new WinGetArguments("pin remove");
248 }
249
256 public static WinGetArguments PinReset()
257 {
258 return new WinGetArguments("pin reset");
259 }
260
268 {
269 return new WinGetArguments("source add");
270 }
271
279 {
280 return new WinGetArguments("source remove");
281 }
282
290 {
291 return new WinGetArguments("source update");
292 }
293
301 {
302 return new WinGetArguments("source reset");
303 }
304
312 {
313 return new WinGetArguments("source export");
314 }
315
331 public static WinGetArguments CustomCmd(string cmd)
332 {
333 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(cmd, "cmd");
334
335 cmd = cmd.ToLower().Trim();
336 if (cmd.StartsWith("winget"))
337 {
338#if NETCOREAPP3_1_OR_GREATER
339 cmd = cmd[6..];
340#elif NETSTANDARD2_0
341 cmd = cmd.Substring(6);
342#endif
343 }
344
345 return new WinGetArguments(cmd);
346 }
347
348 //---Flags-------------------------------------------------------------------------------------
364 public WinGetArguments Custom(string custom)
365 {
366 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(custom, "custom");
367
368 _arguments += $" {custom.ToLower().Trim()}";
369 return this;
370 }
371
390 public WinGetArguments Query(string query)
391 {
392 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(query, "query");
393
394 _arguments += $" --query \"{query}\"";
395 return this;
396 }
397
413 public WinGetArguments Source(string source)
414 {
415 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(source, "source");
416
417 _arguments += $" --source \"{source}\"";
418 return this;
419 }
420
439 public WinGetArguments File(string file)
440 {
441 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(file, "file");
442
443 switch (_action)
444 {
445 case WinGetAction.Hash:
446 _arguments += $" --file \"{file}\"";
447 break;
448 case WinGetAction.Import:
449 _arguments += $" --import-file \"{file}\"";
450 break;
451 case WinGetAction.Export:
452 _arguments += $" --output \"{file}\"";
453 break;
454 }
455
456 return this;
457 }
458
477 public WinGetArguments Directory(string directory)
478 {
479 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(directory, "directory");
480
481 // Remove backslash chars at the end of the path,
482 // because they are not needed and will interfere with winget argument string by negating the last quotation mark char,
483 // used for encasing the directory path.
484#if NETCOREAPP3_1_OR_GREATER
485 while (directory.EndsWith('\\'))
486 {
487 directory = directory[..^1];
488 }
489#elif NETSTANDARD2_0
490 while (directory.EndsWith("\\"))
491 {
492 directory = directory.Substring(0, directory.Length - 1);
493 }
494#endif
495
496 switch (_action)
497 {
498 case WinGetAction.Download:
499 _arguments += $" --download-directory \"{directory}\"";
500 break;
501 }
502
503 return this;
504 }
505
521 public WinGetArguments Enable(string query)
522 {
523 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(query, "query");
524
525 _arguments += $" --enable \"{query}\"";
526 return this;
527 }
528
544 public WinGetArguments Disable(string query)
545 {
546 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(query, "query");
547
548 _arguments += $" --disable \"{query}\"";
549 return this;
550 }
551
570 public WinGetArguments Name(string name)
571 {
572 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(name, "name");
573
574 _arguments += $" --name \"{name}\"";
575 return this;
576 }
577
596 public WinGetArguments Arg(string arg)
597 {
598 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(arg, "arg");
599
600 _arguments += $" --arg \"{arg}\"";
601 return this;
602 }
603
622 public WinGetArguments Type(string type)
623 {
624 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(type, "type");
625
626 _arguments += $" --type \"{type}\"";
627 return this;
628 }
629
645 public WinGetArguments Version(string version)
646 {
647 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(version, "version");
648
649 _arguments += $" --version \"{version}\"";
650 return this;
651 }
652
666 {
667 ArgsHelper.ThrowIfObjectIsNull(version, "version");
668
669 _arguments += $" --version \"{version.ToString()}\"";
670 return this;
671 }
672
680 {
681 _arguments += $" --version";
682 return this;
683 }
684
692 {
693 _arguments += " --exact";
694 return this;
695 }
696
704 {
705 _arguments += " --silent";
706 return this;
707 }
708
716 {
717 _arguments += " --all";
718 return this;
719 }
720
728 {
729 _arguments += " --include-unknown";
730 return this;
731 }
732
740 {
741 _arguments += " --accept-source-agreements";
742 return this;
743 }
744
752 {
753 _arguments += " --accept-package-agreements";
754 return this;
755 }
756
764 {
765 _arguments += " --ignore-unavailable";
766 return this;
767 }
768
776 {
777 _arguments += " --installed";
778 return this;
779 }
780
788 {
789 _arguments += " --blocking";
790 return this;
791 }
792
800 {
801 _arguments += " --force";
802 return this;
803 }
804
812 {
813 _arguments += " --info";
814 return this;
815 }
816
817 //---Others------------------------------------------------------------------------------------
819 public override string ToString()
820 {
821 return _arguments.Trim();
822 }
823 }
824}
Represents a winget arguments string for different winget actions.
static WinGetArguments WinGet()
Creates a new winget arguments object with no base cmd. Used for direct callin of the winget cmd with...
WinGetArguments Disable(string query)
Adds a disable action to the arguments.
static WinGetArguments SourceUpdate()
Creates a new winget arguments object with "source update" as the base cmd.
WinGetArguments All()
Adds the '–all' flag to the arguments.
WinGetArguments Blocking()
Adds the '–blocking' flag to the arguments.
WinGetArguments Name(string name)
Adds name data to the arguments.
WinGetArguments Installed()
Adds the '–installed' flag to the arguments.
static WinGetArguments PinAdd()
Creates a new winget arguments object with "pin add" as the base cmd.
WinGetArguments Version()
Adds a version query to the arguments.
WinGetArguments Custom(string custom)
Adds a custom flag/parameter to the arguments.
WinGetArguments Force()
Adds the '–force' flag to the arguments.
WinGetArguments IgnoreUnavailable()
Adds the '–ignore-unavailable' flag to the arguments.
static WinGetArguments Import()
Creates a new winget arguments object with "import" as the base cmd.
static WinGetArguments SettingsExport()
Creates a new winget arguments object with "settings export" as the base cmd.
WinGetArguments Silent()
Adds the '–silent' flag to the arguments.
string Arguments
Gets the generated arguments.
static WinGetArguments List()
Creates a new winget arguments object with "list" as the base cmd.
WinGetArguments Arg(string arg)
Adds arg (Source argument) data to the arguments.
static WinGetArguments Settings()
Creates a new winget arguments object with "settings" as the base cmd.
static WinGetArguments CustomCmd(string cmd)
Creates a new winget arguments object with a custom base cmd.
WinGetArguments Type(string type)
Adds type data to the arguments.
WinGetArguments IncludeUnknown()
Adds the '–include-unknown' flag to the arguments.
WinGetArguments Info()
Adds the '–info' flag to the arguments.
static WinGetArguments PinList()
Creates a new winget arguments object with "pin list" as the base cmd.
static WinGetArguments Search()
Creates a new winget arguments object with "search" as the base cmd.
WinGetArguments Query(string query)
Adds a query to the arguments.
WinGetArguments Enable(string query)
Adds a enable action to the arguments.
WinGetArguments AcceptSourceAgreements()
Adds the '–accept-source-agreements' flag to the arguments.
WinGetArguments AcceptPackageAgreements()
Adds the '–accept-package-agreements' flag to the arguments.
WinGetArguments Source(string source)
Adds a source query to the arguments.
bool IsEmpty
Gets if the object is empty.
static WinGetArguments SourceReset()
Creates a new winget arguments object with "source reset" as the base cmd.
WinGetArguments Exact()
Adds the '–exact' flag to the arguments.
static WinGetArguments PinRemove()
Creates a new winget arguments object with "pin remove" as the base cmd.
override string ToString()
static WinGetArguments SourceAdd()
Creates a new winget arguments object with "source add" as the base cmd.
static WinGetArguments SourceExport()
Creates a new winget arguments object with "source export" as the base cmd.
WinGetArguments File(string file)
Adds a file path to the arguments.
static WinGetArguments Repair()
Creates a new winget arguments object with "repair" as the base cmd.
static WinGetArguments Download()
Creates a new winget arguments object with "download" as the base cmd.
static WinGetArguments Hash()
Creates a new winget arguments object with "hash" as the base cmd.
WinGetArguments Version(Version version)
Adds a version query to the arguments.
static WinGetArguments Uninstall()
Creates a new winget arguments object with "uninstall" as the base cmd.
WinGetArguments Directory(string directory)
Adds a directory path to the arguments.
static WinGetArguments SourceRemove()
Creates a new winget arguments object with "source remove" as the base cmd.
static WinGetArguments Export()
Creates a new winget arguments object with "export" as the base cmd.
static WinGetArguments Upgrade()
Creates a new winget arguments object with "upgrade" as the base cmd.
static WinGetArguments PinReset()
Creates a new winget arguments object with "pin reset" as the base cmd.
static WinGetArguments Install()
Creates a new winget arguments object with "install" as the base cmd.
WinGetArguments Version(string version)
Adds a version query to the arguments.
Interface for all winget related objects.