CNN national security analyst John Kirby is a retired rear admiral in the US Navy who was a spokesman for both the State and Defense departments in the Obama administration. The views expressed in this commentary are his own.

(CNN) President Trump's speech Wednesday at the Coast Guard Academy got off to a pretty good start, but, sadly, it's only going to be remembered for his complaints about media coverage and the way he believes he has been treated.

And that's a shame.

It's a shame because none of that stuff needed to be said, even with all the Comey-Russia headlines out there. No President -- even in good times -- has ever truly been happy about his media coverage.

It's a shame because the speech could have been an opportunity for the commander in chief to lay out a grander vision for the Coast Guard, or perhaps a new maritime defense strategy. Military academy commencements are great venues to, as we say in the Navy, set a new course.

But mostly, it's a shame because those cadets deserved better than to serve as the backdrop for a presidential pity party. This was no place or time for a political speech.

The ceremony represented the culmination of four hard years of study, work and training. It should have been all about those cadets and their incredible accomplishment. Instead, the President chose to intrude on it to assuage his ego. He took what should have been a big day and made it small.

To be sure, there were people in the audience who appreciated the President's complaints. One could hear applause and cheers as he lambasted his critics. No doubt many cadets and families will remain happy about the day, regardless of how they felt about the President's speech. But that's not the point. A commencement speech ought to give a charge to the graduates, something they can hold on to and look back on fondly, something to inspire them to achieve more, grow more, learn more.

And darn if he didn't seem at first like he was headed there. The first two-thirds of the President's speech was near perfect. He thanked the leadership at the academy and the staff. He led a round of applause for the family members in attendance. He praised the brave and effective work of the Coast Guard and extolled the virtues of public and uniformed service.

"You could have gone to school anywhere you wanted, and with very, very few responsibilities by comparison," he said. "Instead, you chose the path of service. You chose hard work, high standards, and a very noble mission to save lives, defend the homeland, and protect America's interests around the world. ... Good choice."

He demonstrated a decent understanding of the Coast Guard's history and of its missions, detailing for the graduates all the types of things they would soon find themselves doing in far-flung places: drug interdiction, maritime security, lifesaving efforts, humanitarian relief. He even dwelled a moment on the "scourge of human trafficking." This, from a man who has been roundly criticized as either uninterested in -- or ignorant of -- human rights issues.

"In the Coast Guard, you don't run from danger," he noted. "You chase it."

This was a commander in chief. This was a speech on its way to something.

But then he threw the rudder over. Then it became a campaign speech, a rally cry, an airing of grievances.

At a moment when he could have offered positive advice about life, love and leadership, he bragged about his accomplishments in office and groaned about unfairness. He told them they would "find that things happen to you that you do not deserve and that are not always warranted." And he warned them not to "let the critics and the naysayers get in the way of your dreams."

JUST WATCHED Coast Guard, TSA could face deep budget cuts Replay More Videos ... MUST WATCH Coast Guard, TSA could face deep budget cuts 02:39

Now, I wasn't smart enough or fit enough or talented enough to attend one of our service academies. But I have attended plenty of commencements at the Naval Academy in Annapolis, at West Point and at the Air Force Academy, watching as good friends -- and their sons and daughters -- threw those caps into the air. I even wrote a speech or two for my bosses to give at some of those ceremonies.

It's unlike any other type of college graduation, because, well, it isn't just a college graduation. It's a commissioning. For those cadets, it marks the beginning of their service to the nation as officers. That's a big deal. As the President himself noted, these young people will soon be off to advanced training and then to sea. And there, at sea, they will face danger, learn how to lead and help protect our country.

And that's where he should have left it. That should have been his message, his sound bite.

The Coast Guard Academy Class of 2017 got their diplomas from the President of the United States. They'll surely remember that. And I hope they remember all the other stuff he said Wednesday, the good stuff.

Unfortunately, the rest of us will only remember the pettiness. And that's a shame.