xref: /haiku/src/bin/pkgman/command_uninstall.cpp (revision a085e81e62d7a860f809b4fb7c7bf5654c396985)
1 /*
2  * Copyright 2013, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Ingo Weinhold <ingo_weinhold@gmx.de>
7  */
8 
9 
10 #include <getopt.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 
14 #include "Command.h"
15 #include "pkgman.h"
16 #include "PackageManager.h"
17 
18 
19 // TODO: internationalization!
20 
21 
22 using namespace BPackageKit;
23 using namespace BPackageKit::BPrivate;
24 
25 
26 static const char* const kShortUsage =
27 	"  %command% <package> ...\n"
28 	"    Uninstalls one or more packages.\n";
29 
30 static const char* const kLongUsage =
31 	"Usage: %program% %command% <package> ...\n"
32 	"Uninstalls the specified packages.\n"
33 	"\n"
34 	"Options:\n"
35 	"  -H, --home\n"
36 	"    Uninstall the packages from the user's home directory. Default is to\n"
37 	"    uninstall from the common directory.\n"
38 	"\n";
39 
40 
41 DEFINE_COMMAND(UninstallCommand, "uninstall", kShortUsage, kLongUsage,
42 	kCommandCategoryPackages)
43 
44 
45 int
46 UninstallCommand::Execute(int argc, const char* const* argv)
47 {
48 	bool uninstallFromHome = false;
49 
50 	while (true) {
51 		static struct option sLongOptions[] = {
52 			{ "help", no_argument, 0, 'h' },
53 			{ "home", no_argument, 0, 'H' },
54 			{ 0, 0, 0, 0 }
55 		};
56 
57 		opterr = 0; // don't print errors
58 		int c = getopt_long(argc, (char**)argv, "hu", sLongOptions, NULL);
59 		if (c == -1)
60 			break;
61 
62 		switch (c) {
63 			case 'h':
64 				PrintUsageAndExit(false);
65 				break;
66 
67 			case 'H':
68 				uninstallFromHome = true;
69 				break;
70 
71 			default:
72 				PrintUsageAndExit(true);
73 				break;
74 		}
75 	}
76 
77 	// The remaining arguments are the packages to be uninstalled.
78 	if (argc < optind + 1)
79 		PrintUsageAndExit(true);
80 
81 	int packageCount = argc - optind;
82 	const char* const* packages = argv + optind;
83 
84 	// perform the installation
85 	BPackageInstallationLocation location = uninstallFromHome
86 		? B_PACKAGE_INSTALLATION_LOCATION_HOME
87 		: B_PACKAGE_INSTALLATION_LOCATION_COMMON;
88 	PackageManager packageManager(location, true, true);
89 	packageManager.Uninstall(packages, packageCount);
90 
91 	return 0;
92 }
93