2 * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
12 #include <package/PackageInfo.h>
17 using namespace BPackageKit
;
21 command_info(int argc
, const char* const* argv
)
23 const char* format
= "name: %name% version: %version%\n";
26 static struct option sLongOptions
[] = {
27 { "format", required_argument
, 0, 'f' },
28 { "help", no_argument
, 0, 'h' },
32 opterr
= 0; // don't print errors
33 int c
= getopt_long(argc
, (char**)argv
, "f:h", sLongOptions
, NULL
);
43 print_usage_and_exit(true);
48 // One argument should remain -- the package file name.
49 if (optind
+ 1 != argc
)
50 print_usage_and_exit(true);
52 const char* fileName
= argv
[optind
++];
54 // Read the package info from the package file. If it doesn't look like a
55 // package file, assume it is a package info file.
57 if (BString(fileName
).EndsWith(".hpkg")) {
58 status_t error
= info
.ReadFromPackageFile(fileName
);
60 fprintf(stderr
, "Error: Failed to read package file \"%s\": %s\n",
61 fileName
, strerror(error
));
65 status_t error
= info
.ReadFromConfigFile(BEntry(fileName
));
67 fprintf(stderr
, "Error: Failed to read package info file \"%s\": "
68 "%s\n", fileName
, strerror(error
));
73 // parse format string and produce output
75 while (*format
!= '\0') {
80 const char* start
= format
;
81 while (*format
!= '\0' && *format
!= '%')
84 fprintf(stderr
, "Error: Unexpected at end of the format "
85 "string. Expected \"%%\".\n");
89 if (format
== start
) {
92 BString
variable(start
, format
- start
);
93 if (variable
== "fileName") {
94 output
<< info
.CanonicalFileName();
95 } else if (variable
== "name") {
96 output
<< info
.Name();
97 } else if (variable
== "version") {
98 output
<< info
.Version().ToString();
100 fprintf(stderr
, "Error: Unsupported placeholder \"%s\" "
101 "in format string.\n", variable
.String());
113 fprintf(stderr
, "Error: \"\\\" at the end of the format "
133 fputs(output
.String(), stdout
);