Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: show: add --attributes flag #1289

Merged
merged 1 commit into from
Dec 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions src/cli/Show.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ int Show::execute(QStringList arguments)
QObject::tr("Key file of the database."),
QObject::tr("path"));
parser.addOption(keyFile);
QCommandLineOption attributes(QStringList() << "a"
<< "attributes",
QObject::tr("Names of the attributes to show. "
"This option can be specified more than once, with each attribute shown one-per-line in the given order. "
"If no attributes are specified, a summary of the default attributes is given."),
QObject::tr("attribute"));
parser.addOption(attributes);
parser.addPositionalArgument("entry", QObject::tr("Name of the entry to show."));
parser.process(arguments);

Expand All @@ -63,10 +70,10 @@ int Show::execute(QStringList arguments)
return EXIT_FAILURE;
}

return this->showEntry(db, args.at(1));
return this->showEntry(db, parser.values(attributes), args.at(1));
}

int Show::showEntry(Database* database, QString entryPath)
int Show::showEntry(Database* database, QStringList attributes, QString entryPath)
{

QTextStream inputTextStream(stdin, QIODevice::ReadOnly);
Expand All @@ -78,10 +85,24 @@ int Show::showEntry(Database* database, QString entryPath)
return EXIT_FAILURE;
}

outputTextStream << " title: " << entry->title() << endl;
outputTextStream << "username: " << entry->username() << endl;
outputTextStream << "password: " << entry->password() << endl;
outputTextStream << " URL: " << entry->url() << endl;
outputTextStream << " Notes: " << entry->notes() << endl;
return EXIT_SUCCESS;
// If no attributes specified, output the default attribute set.
bool showAttributeNames = attributes.isEmpty();
if (attributes.isEmpty()) {
attributes = EntryAttributes::DefaultAttributes;
}

// Iterate over the attributes and output them line-by-line.
bool sawUnknownAttribute = false;
for (QString attribute : attributes) {
if (!entry->attributes()->contains(attribute)) {
sawUnknownAttribute = true;
qCritical("ERROR: unknown attribute '%s'.", qPrintable(attribute));
continue;
}
if (showAttributeNames) {
outputTextStream << attribute << ": ";
}
outputTextStream << entry->attributes()->value(attribute) << endl;
}
return sawUnknownAttribute ? EXIT_FAILURE : EXIT_SUCCESS;
}
2 changes: 1 addition & 1 deletion src/cli/Show.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Show : public Command
Show();
~Show();
int execute(QStringList arguments);
int showEntry(Database* database, QString entryPath);
int showEntry(Database* database, QStringList attributes, QString entryPath);
};

#endif // KEEPASSXC_SHOW_H
8 changes: 8 additions & 0 deletions src/cli/keepassxc-cli.1
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ Specify the title of the entry.
Perform advanced analysis on the password.


.SS "Show options"

.IP "-a, --attributes <attribute>..."
Names of the attributes to show. This option can be specified more than once,
with each attribute shown one-per-line in the given order. If no attributes are
specified, a summary of the default attributes is given.


.SH REPORTING BUGS
Bugs and feature requests can be reported on GitHub at https://github.com/keepassxreboot/keepassxc/issues.

Expand Down