1//
2// TwitterApp.cpp
3//
4// A very simple command-line Twitter client.
5//
6// Copyright (c) 2009-2013, Applied Informatics Software Engineering GmbH.
7// and Contributors.
8//
9// SPDX-License-Identifier: BSL-1.0
10//
11
12
13#include "Poco/Util/Application.h"
14#include "Poco/Util/Option.h"
15#include "Poco/Util/OptionSet.h"
16#include "Poco/Util/HelpFormatter.h"
17#include "Twitter.h"
18#include <iostream>
19
20
21using Poco::Util::Application;
22using Poco::Util::Option;
23using Poco::Util::OptionSet;
24using Poco::Util::HelpFormatter;
25using Poco::Util::AbstractConfiguration;
26using Poco::Util::OptionCallback;
27
28
29class TweetApp: public Application
30 /// A very simple Twitter command-line client.
31{
32public:
33 TweetApp()
34 {
35 }
36
37protected:
38 void defineOptions(OptionSet& options)
39 {
40 Application::defineOptions(options);
41
42 options.addOption(
43 Option("help", "h", "Display help information on command line arguments.")
44 .required(false)
45 .repeatable(false)
46 .callback(OptionCallback<TweetApp>(this, &TweetApp::handleHelp)));
47
48 options.addOption(
49 Option("message", "m", "Specify the status message to post.")
50 .required(true)
51 .repeatable(false)
52 .argument("message")
53 .callback(OptionCallback<TweetApp>(this, &TweetApp::handleMessage)));
54
55 options.addOption(
56 Option("ckey", "c", "Specify the Twitter consumer key.")
57 .required(true)
58 .repeatable(false)
59 .argument("consumer key")
60 .callback(OptionCallback<TweetApp>(this, &TweetApp::handleConsumerKey)));
61
62 options.addOption(
63 Option("csecret", "s", "Specify the Twitter consumer secret.")
64 .required(true)
65 .repeatable(false)
66 .argument("consumer secret")
67 .callback(OptionCallback<TweetApp>(this, &TweetApp::handleConsumerSecret)));
68
69 options.addOption(
70 Option("token", "t", "Specify the Twitter access token.")
71 .required(true)
72 .repeatable(true)
73 .argument("access token")
74 .callback(OptionCallback<TweetApp>(this, &TweetApp::handleAccessToken)));
75
76 options.addOption(
77 Option("tsecret", "S", "Specify the Twitter access token secret.")
78 .required(true)
79 .repeatable(true)
80 .argument("access token secret")
81 .callback(OptionCallback<TweetApp>(this, &TweetApp::handleAccessTokenSecret)));
82 }
83
84 void handleHelp(const std::string& name, const std::string& value)
85 {
86 displayHelp();
87 stopOptionsProcessing();
88 }
89
90 void handleConsumerKey(const std::string& name, const std::string& value)
91 {
92 _consumerKey = value;
93 }
94
95 void handleConsumerSecret(const std::string& name, const std::string& value)
96 {
97 _consumerSecret = value;
98 }
99
100 void handleAccessToken(const std::string& name, const std::string& value)
101 {
102 _accessToken = value;
103 }
104
105 void handleAccessTokenSecret(const std::string& name, const std::string& value)
106 {
107 _accessTokenSecret = value;
108 }
109
110 void handleMessage(const std::string& name, const std::string& value)
111 {
112 _message = value;
113 }
114
115 void displayHelp()
116 {
117 HelpFormatter helpFormatter(options());
118 helpFormatter.setCommand(commandName());
119 helpFormatter.setUsage("OPTIONS");
120 helpFormatter.setHeader("A simple Twitter command line client for posting status updates.");
121 helpFormatter.format(std::cout);
122 }
123
124 int main(const std::vector<std::string>& args)
125 {
126 try
127 {
128 if (!_message.empty())
129 {
130 Twitter twitter;
131 twitter.login(_consumerKey, _consumerSecret, _accessToken, _accessTokenSecret);
132 Poco::Int64 statusId = twitter.update(_message);
133 std::cout << statusId << std::endl;
134 }
135 }
136 catch (Poco::Exception& exc)
137 {
138 std::cerr << exc.displayText() << std::endl;
139 return Application::EXIT_SOFTWARE;
140 }
141 return Application::EXIT_OK;
142 }
143
144private:
145 std::string _consumerKey;
146 std::string _consumerSecret;
147 std::string _accessToken;
148 std::string _accessTokenSecret;
149 std::string _message;
150};
151
152
153POCO_APP_MAIN(TweetApp)
154