xref: /haiku/src/bin/pkgman/command_install.cpp (revision 4a3268e14fff4dd5a456d824b48ce6503368e4c1)
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 	"    Installs one or more packages.\n";
29 
30 static const char* const kLongUsage =
31 	"Usage: %program% %command% <package> ...\n"
32 	"Installs the specified packages.\n"
33 	"\n"
34 	"Options:\n"
35 	"  -H, --home\n"
36 	"    Install the packages in the user's home directory. Default is to\n"
37 	"    install in the system directory.\n"
38 	"  -y\n"
39 	"    Non-interactive mode. Automatically confirm changes, but fail when\n"
40 	"    encountering problems.\n"
41 	"\n";
42 
43 
44 DEFINE_COMMAND(InstallCommand, "install", kShortUsage, kLongUsage,
45 	kCommandCategoryPackages)
46 
47 
48 int
49 InstallCommand::Execute(int argc, const char* const* argv)
50 {
51 	BPackageInstallationLocation location
52 		= B_PACKAGE_INSTALLATION_LOCATION_SYSTEM;
53 	bool interactive = true;
54 
55 	while (true) {
56 		static struct option sLongOptions[] = {
57 			{ "help", no_argument, 0, 'h' },
58 			{ "home", no_argument, 0, 'H' },
59 			{ 0, 0, 0, 0 }
60 		};
61 
62 		opterr = 0; // don't print errors
63 		int c = getopt_long(argc, (char**)argv, "hHy", sLongOptions, NULL);
64 		if (c == -1)
65 			break;
66 
67 		switch (c) {
68 			case 'h':
69 				PrintUsageAndExit(false);
70 				break;
71 
72 			case 'H':
73 				location = B_PACKAGE_INSTALLATION_LOCATION_HOME;
74 				break;
75 
76 			case 'y':
77 				interactive = false;
78 				break;
79 
80 			default:
81 				PrintUsageAndExit(true);
82 				break;
83 		}
84 	}
85 
86 	// The remaining arguments are the packages to be installed.
87 	if (argc < optind + 1)
88 		PrintUsageAndExit(true);
89 
90 	int packageCount = argc - optind;
91 	const char* const* packages = argv + optind;
92 
93 	// perform the installation
94 	PackageManager packageManager(location, interactive);
95 	packageManager.Install(packages, packageCount);
96 
97 	return 0;
98 }
99